Android仿京东左右分类

最近看到很多购物商城都是采用了一种布局,左边是list列表右侧是商品的列表

Android仿京东左右分类_第1张图片

所以练练手,其实很简单,左边就是一个ListView,右边是一个Fragment,Fragment的内容可以自己定义,用LinearLayout来关联,使用android:layout_weight来分割大小,看代码吧




    

        
    

    

    

    

        

        

        
    


有了这个基本上就完成了,但是为了测试,可以继续看Adapter

import com.example.listviewfragmentdemo.MainActivity;
import com.example.listviewfragmentdemo.R;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class MyAdapter extends BaseAdapter {

	private Context context;
	private String[] strings;
	public static int mPosition;
	
	public MyAdapter(Context context, String[] strings){
		this.context =context;
		this.strings = strings;
	}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return strings.length;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return strings[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
		convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
		TextView tv = (TextView) convertView.findViewById(R.id.tv);
		mPosition = position;
		tv.setText(strings[position]);
		if (position == MainActivity.mPosition) {
			convertView.setBackgroundResource(R.drawable.tongcheng_all_bg01);
		} else {
			convertView.setBackgroundColor(Color.parseColor("#f4f4f4"));
		}
		return convertView;
	}
}

还需要一个Fragment

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment extends Fragment {
	
	public static final String TAG = "MyFragment";
	private String str;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.myfragment, null);
		TextView tv_title = (TextView) view.findViewById(R.id.tv_title);
		//得到数据
		str = getArguments().getString(TAG);
		tv_title.setText(str);
		return view;
	}
}

最后是MainActivity

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

/**
 * 
 * @author qdwang
 *
 */
public class MainActivity extends FragmentActivity implements
		OnItemClickListener {

	private String[] strs = { "常用分类", "服饰内衣", "鞋靴", "手机", "家用电器", "数码", "电脑办公",
			"个护化妆", "图书" };
	private ListView listView;
	private MyAdapter adapter;
	private MyFragment myFragment;
	public static int mPosition;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initView();
	}

	/**
	 * 初始化view
	 */
	private void initView() {
		// TODO Auto-generated method stub
		listView = (ListView) findViewById(R.id.listview);

		adapter = new MyAdapter(this, strs);
		listView.setAdapter(adapter);

		listView.setOnItemClickListener(this);

		//创建MyFragment对象
		myFragment = new MyFragment();
		FragmentTransaction fragmentTransaction = getSupportFragmentManager()
				.beginTransaction();
		fragmentTransaction.replace(R.id.fragment_container, myFragment);
		//通过bundle传值给MyFragment
		Bundle bundle = new Bundle();
		bundle.putString(MyFragment.TAG, strs[mPosition]);
		myFragment.setArguments(bundle);
		fragmentTransaction.commit();
	}

	@Override
	public void onItemClick(AdapterView parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		//拿到当前位置
		mPosition = position;
		//即使刷新adapter
		adapter.notifyDataSetChanged();
		for (int i = 0; i < strs.length; i++) {
			myFragment = new MyFragment();
			FragmentTransaction fragmentTransaction = getSupportFragmentManager()
					.beginTransaction();
			fragmentTransaction.replace(R.id.fragment_container, myFragment);
			Bundle bundle = new Bundle();
			bundle.putString(MyFragment.TAG, strs[position]);
			myFragment.setArguments(bundle);
			fragmentTransaction.commit();
		}
	}
}


要是喜欢上我的资源列表页下载

你可能感兴趣的:(Android)