XListView多條目展示和TabLayout滑動
阿新 • • 發佈:2018-12-21
MainActivity頁面 public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private FrameLayout framelayout; private ImageView img_view; private TextView xlistView; private TextView tabLayout; private LinearLayout left_layout; private DrawerLayout drawer_layout; private FragmentManager manager; private ASFragment aFragment; private BFragment bFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); //資料來源 initData(); //查詢控制元件 initView(); } private void initData() { aFragment = new ASFragment(); bFragment = new BFragment(); } private void initView() { framelayout = (FrameLayout) findViewById(R.id.framelayout); img_view = (ImageView) findViewById(R.id.img_view); xlistView = (TextView) findViewById(R.id.xlistView); xlistView.setOnClickListener(this); tabLayout = (TextView) findViewById(R.id.tabLayout); tabLayout.setOnClickListener(this); left_layout = (LinearLayout) findViewById(R.id.left_layout); drawer_layout = (DrawerLayout) findViewById(R.id.drawer_layout); manager = getSupportFragmentManager(); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.xlistView: //開啟事務,公用一個manager manager.beginTransaction().replace(R.id.framelayout,aFragment).commit(); drawer_layout.closeDrawers();//關閉側滑頁面 break; case R.id.tabLayout: manager.beginTransaction().replace(R.id.framelayout,bFragment).commit(); drawer_layout.closeDrawers(); break; } }
}
main_activity.xml頁面 引入design <android.support.v4.widget.DrawerLayout xmlns:android=“http://schemas.android.com/apk/res/android” android:layout_width=“match_parent” android:layout_height=“match_parent” android:id="@+id/drawer_layout">
<RelativeLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/framelayout"> </FrameLayout> </RelativeLayout> <LinearLayout android:orientation="vertical" android:layout_width="300dp" android:background="@color/colorPrimary" android:layout_gravity="start" android:layout_height="match_parent" android:id="@+id/left_layout"> <ImageView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/img_view" android:gravity="center_horizontal" android:src="@mipmap/ic_launcher_round"/> <TextView android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/xlistView" android:gravity="center_horizontal" android:text="xlistview" android:layout_width="match_parent" android:layout_height="wrap_content" /> <TextView android:id="@+id/tabLayout" android:gravity="center_horizontal" android:text="tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
</android.support.v4.widget.DrawerLayout>
XListViewFragment頁面 public class ASFragment extends Fragment implements XListView.IXListViewListener {
private String mUrl = "http://www.xieast.com/api/news/news.php?page="; private MyAdapter adapter; private List<MyData.DataBean> data = new ArrayList<>(); private XListView xlist_view; private int index = 1; private View v; private Context context; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { v = inflater.inflate(R.layout.fragment_a,null); //查詢控制元件 initView(v); context = getActivity().getApplicationContext(); adapter = new MyAdapter(data,context); xlist_view.setAdapter(adapter); new MyTask().execute(mUrl+index); return v; } private void initView(View v) { xlist_view = v.findViewById(R.id.xlist_view); xlist_view.setPullLoadEnable(true); xlist_view.setXListViewListener(this); } @Override public void onRefresh() { //重新整理 data.clear(); new MyTask().execute(mUrl+1); } public void close(){ xlist_view.stopRefresh(); xlist_view.stopLoadMore(); } @Override public void onLoadMore() { //載入更多 new MyTask().execute(mUrl+(++index)); } class MyTask extends AsyncTask<String,Void,List<MyData.DataBean>> { @Override protected List<MyData.DataBean> doInBackground(String... strings) { try { String jsonStr = Utils.get(strings[0]); Gson gson = new Gson(); MyData data = gson.fromJson(jsonStr, MyData.class); return data.getData(); } catch (Exception e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(List<MyData.DataBean> dataBeans) { super.onPostExecute(dataBeans); data.addAll(dataBeans); adapter.notifyDataSetChanged(); close(); } }
} XListViewFragment.xml頁面
<com.example.mylibrary.view.XListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/xlist_view">
</com.example.mylibrary.view.XListView>
**TabLayoutFragment頁面**
public class BFragment extends Fragment {
private View v;
private TabLayout tabLayout;
private ViewPager viewPager;
private List<Fragment> mLists = new ArrayList<>();
private List<String> mTitle = new ArrayList<>();
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v = inflater.inflate(R.layout.fragment_b,null);
initData();
initView();
return v;
}
private void initData() {
mLists.add(new ASFragment());
mLists.add(new ASFragment());
mTitle.add("新聞");
mTitle.add("娛樂");
}
private void initView() {
tabLayout = v.findViewById(R.id.tab_layout);
viewPager = v.findViewById(R.id.view_pager);
//給viewpager設定介面卡
viewPager.setAdapter(new MyPageAdapter(getChildFragmentManager(),mLists,mTitle));
//通過setupWithViewPager將TabLayout和ViewPager建立關聯
//在Adapter中複寫PagerAdapter中的getPageTitle方法
tabLayout.setupWithViewPager(viewPager);
}
}
TabLayoutFragment.xml頁面
<!-- app:tabMode="scrollable" 會隨著滑動而變動-->
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="scrollable"
android:id="@+id/tab_layout"/>
<android.support.v4.view.ViewPager
android:layout_below="@id/tab_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/view_pager">
</android.support.v4.view.ViewPager>
展示ListView多條目列表介面卡
public MyAdapter(ArrayList<MyData.DataBean> mList, Context mContext) {
this.mList = mList;
this.mContext = mContext;
mDisImageOp = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.ic_launcher)
.showImageForEmptyUri(R.mipmap.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.cacheInMemory(true)
.cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.ARGB_8888) //設定圖片的解碼型別
.displayer(new CircleBitmapDisplayer())//載入圓角圖片
.build();
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
HolderOne one = null;
HolderTwo two = null;
int type = getItemViewType(position);
MyData.DataBean dataBean = mList.get(position);
switch (type){
case ITEM_ONE:
if (convertView == null){
one = new HolderOne();
convertView = View.inflate(mContext,R.layout.item_one,null);
one.mNameOne = convertView.findViewById(R.id.Item_One_Text);
one.mImageOne = convertView.findViewById(R.id.Item_One_Image);
convertView.setTag(one);
}else {
one = (HolderOne) convertView.getTag();
}
one.mNameOne.setText(dataBean.getTitle()+"");
mImageLoader.displayImage(dataBean.getThumbnail_pic_s(),one.mImageOne,mDisImageOp);
break;
case ITEM_TWO:
if (convertView == null){
two = new HolderTwo();
convertView = View.inflate(mContext,R.layout.item_two,null);
two.mNameTwo = convertView.findViewById(R.id.Item_Two_Text);
two.mImageTwo = convertView.findViewById(R.id.Item_Two_Image);
convertView.setTag(two);
}else {
two = (HolderTwo) convertView.getTag();
}
two.mNameTwo.setText(dataBean.getTitle()+"");
mImageLoader.displayImage(dataBean.getThumbnail_pic_s(),two.mImageTwo,mDisImageOp);
break;
}
return convertView;
}
//返回條目的型別
@Override
public int getItemViewType(int position) {
//比如正常的介面一般都是有欄位判斷的 data.getType =1
if (position % 2 == 0) {
return ITEM_ONE;
} else {
return ITEM_TWO;
}
}
@Override
public int getViewTypeCount() {
return 2;
}
class HolderOne {
private TextView mNameOne;
private ImageView mImageOne;
}
class HolderTwo {
private TextView mNameTwo;
private ImageView mImageTwo;
}
}
TabLayout滑動介面卡
public class MyPageAdapter extends FragmentPagerAdapter {
private List<Fragment> mLists ;
private List<String> mTitle ;
public MyPageAdapter(FragmentManager fm,List<Fragment> mlist,List<String> mtitle) {
super(fm);
this.mLists = mlist;
this.mTitle = mtitle;
}
@Override
public Fragment getItem(int i) {
return mLists.get(i);
}
@Override
public int getCount() {
return mLists.size();
}
@Nullable
@Override//在Adapter中複寫PagerAdapter中的getPageTitle方法
public CharSequence getPageTitle(int position) {
return mTitle.get(position);
}
}