viewpageindicator

<com.viewpagerindicator.TabPageIndicator
        android:id="@+id/indicator"
        android:background="@drawable/base_action_bar_bg"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        />
    <android.support.v4.view.ViewPager
        android:id="@+id/pager"
        android:layout_width="fill_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        />
//主界面
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.administrator.yunifang.R;
import com.example.administrator.yunifang.bean.Fenleibean;
import com.example.administrator.yunifang.fragment.Fragment1;
import com.viewpagerindicator.TabPageIndicator;
import java.util.List;

public class ShangpingfeileiActivity extends FragmentActivity  {
    private List<Fenleibean.DataEntity.CategoryEntity.ChildrenEntity> TITLE;
    private ImageView fh;
    private TextView an;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_shangpingfeilei);
            fh= (ImageView) findViewById(R.id.remen_fh);
            fh.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
            an= (TextView) findViewById(R.id.anfenlei);
            String anname=getIntent().getStringExtra("name");
            an.setText(anname);
            TITLE= (List<Fenleibean.DataEntity.CategoryEntity.ChildrenEntity>) getIntent().getSerializableExtra("bean");
            //ViewPageradapter
            FragmentPagerAdapter adapter = new TabPageIndicatorAdapter(getSupportFragmentManager());
            ViewPager pager = (ViewPager)findViewById(R.id.pager);
            pager.setAdapter(adapter);

            //实例化TabPageIndicator然后设置ViewPager与之关联
            TabPageIndicator indicator = (TabPageIndicator)findViewById(R.id.indicator);
            indicator.setViewPager(pager);

        }


        /**
         * ViewPager适配器
         * @author len
         *
         */
        class TabPageIndicatorAdapter extends FragmentPagerAdapter {
            public TabPageIndicatorAdapter(FragmentManager fm) {
                super(fm);
            }

            @Override
            public Fragment getItem(int position) {
                //新建一个Fragment来展示ViewPager item的内容,并传递参数
                Fragment fragment = new Fragment1();
                Bundle args = new Bundle();
                args.putString("arg", TITLE.get(position).id);
                fragment.setArguments(args);

                return fragment;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return TITLE.get(position).cat_name;
            }

            @Override
            public int getCount() {
                return TITLE.size();
            }
        }

    }
//Fragmen1文件
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import com.example.administrator.yunifang.R;
import com.example.administrator.yunifang.activity.ShouyegridviewActivity;
import com.example.administrator.yunifang.adapter.SpflAdapter;
import com.example.administrator.yunifang.bean.Shangpingfellei;
import com.example.administrator.yunifang.http.Httpurlconnection;
import com.google.gson.Gson;
import java.util.List;

/**
 * Created by Administrator on 2016/9/6.
 */
public class Fragment1 extends Fragment {
    private String url="http://m.yunifang.com/yunifang/mobile/goods/getall?random=97575&encode=f1c61556b05c0cf193e74b1e8fc6420c&category_id=";
    private GridView gv;
    private List<Shangpingfellei.DataEntity> tu;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 0:
                    String strr= (String) msg.obj;
                    //Toast.makeText(getActivity(),strr,Toast.LENGTH_SHORT).show();
                    Shangpingfellei vpBean =tojson(strr);//首页listview图片
                    tu=vpBean.data;
                    SpflAdapter shangpingAdapter=new SpflAdapter(tu,getActivity());
                    gv.setAdapter(shangpingAdapter);
                    gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                            Intent intent=new Intent(getActivity(), ShouyegridviewActivity.class);
                            intent.putExtra("id",tu.get(position).id);
                            startActivity(intent);
                        }
                    });
                    break;
                default:
                    break;
            }
        }
    };
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View contextView = inflater.inflate(R.layout.fragment1, container, false);
            //获取Activity传递过来的参数
            Bundle mBundle = getArguments();
            String title = mBundle.getString("arg");
            //Toast.makeText(getActivity(), title, Toast.LENGTH_SHORT).show();
            gv= (GridView) contextView.findViewById(R.id.spfenleigv);
            final String urla=url+title;
            //请求数据
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    String str= Httpurlconnection.getid(urla);
                    Message message = new Message();
                    message.what = 0;
                    message.obj = str;
                    handler.sendMessage(message);
                }
            }.start();
        return contextView;
    }

        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);
        }
    //解析json文件
    private Shangpingfellei tojson(String str) {
        Gson g = new Gson();
        Shangpingfellei vpBean = g.fromJson(str, Shangpingfellei.class);
        //Toast.makeText(getActivity(),as.toString(),Toast.LENGTH_SHORT).show();
        return vpBean;
    }
}
//配置清单文件
<activity android:name=".activity.ShangpingfeileiActivity" android:theme="@style/StyledIndicators"/>
//values中styles
<!--ViewPageIndicator-->
<style name="StyledIndicators" parent="AppTheme">
    <item name="vpiTabPageIndicatorStyle">@style/CustomTabPageIndicator</item>
</style>

<style name="CustomTabPageIndicator" parent="Widget.TabPageIndicator">
    <item name="android:background">@drawable/tab_indicator</item>
    <item name="android:textAppearance">@style/CustomTabPageIndicator.Text</item>
    <item name="android:textSize">14sp</item>
    <item name="android:dividerPadding">8dp</item>
    <item name="android:showDividers">middle</item>
    <item name="android:paddingLeft">10dp</item>
    <item name="android:paddingRight">10dp</item>
    <item name="android:fadingEdge">horizontal</item>
    <item name="android:fadingEdgeLength">8dp</item>
</style>

<style name="CustomTabPageIndicator.Text" parent="android:TextAppearance.Medium">
    <item name="android:typeface">monospace</item>
    <item name="android:textColor">@drawable/selector_tabtext</item>
</style>

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值