49 Android fragment 自定义适配器adapter

item.xml (加载到适配器的布局文件)




    



aitic.xml (加载到 fragment 布局文件)

 


 

     
     

    
 
 


ArticListFragment extends ListFragment

ArticListFragment.java 文件代码

package com.example.android_fragment_adapter;

import java.util.ArrayList;
import java.util.List;

import android.R.array;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ArticListFragment extends ListFragment {

	private MyAdaper adapter;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		adapter=new MyAdaper();
		//绑定数据源
		adapter.bindData(getData());
	}
	
	
	public List getData(){
		List list=new ArrayList();
		for(int i=0;i<20;i++){
			list.add("XXX"+i);
		}
		return list;
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view=inflater.inflate(R.layout.aitic, null);
//		ListView listView=(ListView)view.findViewById(R.id.listView);
//		listView.setAdapter(adapter);//错误的写法
		//绑定适配器
		setListAdapter(adapter);
		return view;
	}
	
	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}
	
	/**
	 * 点击事件的处理
	 */
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		String item=adapter.getItem(position).toString();
		Toast.makeText(getActivity(), item, 1).show();
	}
	/**
	 * 自定义适配器
	 * @author Administrator
	 *
	 */
	class MyAdaper extends BaseAdapter{

		private List list;
		public void bindData(List list ){
			this.list=list;
		}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView textView=null;
			View view=null;
			if(convertView==null)
			{
			   view=LayoutInflater.from(getActivity()).inflate(R.layout.item, null);
			}else{
				view=convertView;
			}
			textView=(TextView)view.findViewById(R.id.textView1_itemxml);
			textView.setText(list.get(position).toString());
			
			return view;
		}
		
	}
}


activity_main.xml 主布局文件(两个 LinearLayout 布局)



    

        

MainActivity.java 文件代码

package com.example.android_fragment_adapter;

import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

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);
		button=(Button)this.findViewById(R.id.button1);
		manager=this.getFragmentManager();
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				transaction=manager.beginTransaction();
				ArticListFragment fragment=new ArticListFragment();
				transaction.add(R.id.right_LinearLayout, fragment, "rifht_fragment");
				transaction.commit();
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}


49 Android fragment 自定义适配器adapter_第1张图片


49 Android fragment 自定义适配器adapter_第2张图片





你可能感兴趣的:(Android)