12. Dp Notes content - list (used by ListView)

        After completing some empty shelves, start filling in the content. Let’s start with the list (the home page has no idea). The list is very simple, a ListView (the kind that can slide all the way from top to bottom, and the layout of each block is very similar). The use of ListView is very similar to that of ViewPager, mainly Adapter. First introduce ListView in the layout:
<ListView
    android:id="@+id/lv_list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/header_list" />

        The ListView object lv_list is defined in ListFragment, which is imported from xml, not much to say. One of the main methods of ListView is setAdapter, which can control the number of items displayed by ListView, layout and so on through Adapter. Create a new package com.zdphpn.dpnotes.adapter, a new class ListAdapter in the package, inherited from BaseAdapter, there are four methods that must be implemented, two of which are important, getCount(), the number of items displayed in ListView, getView(), each The content displayed by an Item, the return value View (can be returned by converting an xml to a View). In addition, add another constructor:
private Activity activity;
public ListAdapter(Activity activity){
    this.activity=activity;
}

        Construct the parameter Activity activity (used when xml is converted to View, so the constructor passes one in, or it can be other Context, etc.). First build a layout file item_list.xml for ListView Item, and write the content at will:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingLeft="@dimen/padding_s"
    android:paddingRight="@dimen/padding_s"
    android:paddingTop="@dimen/padding_ss"
    android:paddingBottom="@dimen/padding_ss" >
    
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        android:padding="@dimen/padding_n"
        android:background="@color/white_dark" >
    
        <TextView
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:text="左" />
        <TextView
            android:layout_width="wrap_content"
	    	android:layout_height="wrap_content"
	    	android:layout_centerInParent="true"
	    	android:text="ListView Item" />

    </RelativeLayout>

</RelativeLayout>

        Leave some space left and right, and the scroll bar of the ListView is located outside the Item. ListAdapter's getView() reference:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    if(convertView==null){
        convertView=activity.getLayoutInflater().inflate(
R.layout.item_list, parent, false);
    }
    else{
        ;
    }
    return convertView;
}

        The xml is converted to View and returned. ConvertView does not say anything first, and the return value of the getCount() function is changed to a non-zero value. Get the ListView object in ListFragment and set the Adapter:
private View view;
private ListView lv_list;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    view=inflater.inflate(R.layout.fragment_list, container, false);
		
    lv_list=(ListView)view.findViewById(R.id.lv_list);
    lv_list.setAdapter(new ListAdapter(getActivity()));
		
    return view;
}

        Note the view.find (why?). run.

Note: This is a .gif animation, ctrl click on the picture to view. The layout of the item has been changed, and it is no longer posted. Is it easy to use ListView?

Consistent - 2016/11/01




Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326203445&siteId=291194637