<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"); //ViewPager的adapter 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>