Android仿京东分类

作者: b812b5a08a93 | 来源:发表于2016-11-23 14:18 被阅读2629次

    最近工作上也不忙,抽空学习学习,就想到了这个效果,写出来供分享,代码写的有点粗,不过能看懂,下面将一些核心代码贴出来供参考,扫描下方二维码,获取完整源码。

    效果图
    screenshot.png
    核心代码
    activity_main.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <RelativeLayout
            android:id="@+id/jzfw_top_layout"
            android:layout_width="match_parent"
            android:layout_height="42dp"
            android:background="@drawable/home_title"
            android:gravity="center_vertical" >
    
            <TextView
                android:id="@+id/jzfw_top_layout_02"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:text="仿京东分类"
                android:textColor="#ffffff"
                android:textSize="20sp" />
        </RelativeLayout>
    
        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#cdcdcd" />
    
        <TextView
            android:layout_width="match_parent"
            android:layout_height="40dp"
            android:background="#ededed"
            android:gravity="center"
            android:text="全部分类"
            android:textColor="#333333"
            android:textSize="17sp" />
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="#fbfbfb"
            android:orientation="horizontal" >
    
            <ListView
                android:id="@+id/listview"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:scrollbars="none"
                android:layout_weight="1.0"
                android:background="#f4f4f4" />
    
            <View
                android:layout_width="1dp"
                android:layout_height="match_parent"
                android:background="#cdcdcd" />
    
            <FrameLayout
                android:id="@+id/fragment_container"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="3.0" />
        </LinearLayout>
    
    </LinearLayout>
    
    listview_item.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:minHeight="60dp"
        android:gravity="center"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv"
            android:layout_width="wrap_content"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    myfragment.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    
    </LinearLayout>
    
    MainActivity.java
    package com.example.listviewfragmentdemo;
    
    import com.example.listviewfragmentdemo.adapter.MyAdapter;
    
    import android.os.Bundle;
    import android.support.v4.app.FragmentActivity;
    import android.support.v4.app.FragmentTransaction;
    import android.view.View;
    import android.view.Window;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ListView;
    
    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);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏  
            setContentView(R.layout.activity_main);
            initView();
        }
    
        /**
         * 初始化view
         */
        private void initView() {
            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) {
            //拿到当前位置
            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();
            }
        }
    }
    
    MyAdapter.java
    package com.example.listviewfragmentdemo.adapter;
    
    import com.example.listviewfragmentdemo.MainActivity;
    import com.example.listviewfragmentdemo.R;
    import android.annotation.SuppressLint;
    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;
    
    @SuppressLint("ViewHolder")
    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() {
            return strings.length;
        }
    
        @Override
        public Object getItem(int position) {
            return strings[position];
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            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;
        }
    }
    
    MyFragment.java
    package com.example.listviewfragmentdemo;
    
    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) {
            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;
        }
    }
    

    扫描下方二维码,获取完整源码

    欢迎加入QQ群和关注微信公众号

    请不要重复添加
    一群:259182457       二群:471496394
    三群:137038987       四群:256636915

    IT专栏微信公众号.jpg

    相关文章

      网友评论

        本文标题:Android仿京东分类

        本文链接:https://www.haomeiwen.com/subject/dghvpttx.html