上一篇文章介绍了ListFragment,其中的ListView并没有自定义适配器,实际上在实际开发中常会用到自定义适配器,是实现更复杂的列表数据展示。所以这篇文章增加了自定义适配器,来进行ListView数据的展示。
实现效果图:
左边是Activity中的一个按钮,点击按钮会出现右边的Fragment相应的数据列表。
代码展示:
布局文件:
activity_main:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:id="@+id/left" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:orientation="vertical" android:background="#DDCCFF"> <Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="显示列表" /> </LinearLayout> <LinearLayout android:id="@+id/right" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="2" android:orientation="vertical" android:background="#DDFFCC"> </LinearLayout> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <ListView android:id="@id/android:list" android:layout_width="match_parent" android:layout_height="wrap_content" > </ListView> </LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20sp"/> </LinearLayout>代码文件:
package com.fragmentdemo9_listfragment2; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.os.Bundle; import android.view.View; import android.widget.Button; /** *ListFragment的实例Demo优化版一。 * */ public class MainActivity extends Activity { private Button button; private FragmentManager manager; private FragmentTransaction transaction; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); manager = getFragmentManager(); button = (Button) findViewById(R.id.button1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { transaction = manager.beginTransaction(); ArticleListFragment articleListFragment = new ArticleListFragment(); transaction.replace(R.id.right, articleListFragment, "right"); transaction.commit(); } }); } }
package com.fragmentdemo9_listfragment2; import java.util.ArrayList; import android.app.ListFragment; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ListView; import android.widget.Toast; /** * 本例中的唯一一个Fragment * @author Administrator * */ public class ArticleListFragment extends ListFragment { private MyAdapter adapter; private ArrayList<String> list = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); for (int i = 0; i < 30; i++) { list.add("item" + i); } adapter = new MyAdapter(getActivity(), list); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.article, null); setListAdapter(adapter); return view; } @Override public void onListItemClick(ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); Toast.makeText(getActivity(), list.get(position), Toast.LENGTH_SHORT).show(); } }
package com.fragmentdemo9_listfragment2; import java.util.ArrayList; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; /** * 自定义适配器MyAdapter * */ public class MyAdapter extends BaseAdapter { private ArrayList<String> list; private Context context; public MyAdapter(Context context, ArrayList<String> list) { this.list = list; this.context = context; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = new ViewHolder(); ; if (convertView == null) { convertView = LayoutInflater.from(context).inflate(R.layout.item, null); holder.textView = (TextView) convertView .findViewById(R.id.textView); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.textView.setText(list.get(position).toString()); return convertView; } class ViewHolder { TextView textView; } }
点击下载源码