Tablayout+Viewpager+recyclerview簡單實現
阿新 • • 發佈:2019-01-10
//小菜鳥奮鬥中
package com.tablayoutviewpagerrecyclerview.tablayoutviewpagerrecyclerview; import android.os.Bundle; import android.support.design.widget.TabLayout; import android.support.v4.app.Fragment; import android.support.v4.view.ViewPager; import android.support.v7.app.AppCompatActivity; import java.util.ArrayList; importjava.util.List; import butterknife.BindView; import butterknife.ButterKnife; /******************************************************* * @author: wukai * * * * * * @date: 2017/9/28 * * * * @description: * * * * * * @version 1.0 * * * * ** *******************************************************/ public class MainActivity extends AppCompatActivity { @BindView(R.id.tabs) TabLayout tabs; @BindView(R.id.viewpager) ViewPager viewpager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); init_ViewPager(); } private void init_ViewPager() { List<String> titles = new ArrayList<>(); tabs = (TabLayout) findViewById(R.id.tabs); viewpager= (ViewPager) findViewById(R.id.viewpager); titles.add("吃"); titles.add("喝"); titles.add("玩"); titles.add("樂"); for (int i = 0; i < titles.size(); i++) { tabs.addTab(tabs.newTab().setText(titles.get(i))); } List<Fragment> fragments = new ArrayList<>(); for (int i = 0; i < titles.size(); i++) { fragments.add(new com.tablayoutviewpagerrecyclerview.tablayoutviewpagerrecyclerview.ListFragment()); } //介面卡 Fragement_testAdapter fragementAdapter = new Fragement_testAdapter(getSupportFragmentManager(), fragments, titles); //給ViewPager設定adapter viewpager.setAdapter(fragementAdapter); //將Tablayout和ViewPager關聯起來 tabs.setupWithViewPager(viewpager); //給TaBlayout設定介面卡 tabs.setTabsFromPagerAdapter(fragementAdapter); }
}
===========================================================================================
package com.tablayoutviewpagerrecyclerview.tablayoutviewpagerrecyclerview; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; /******************************************************* * @author: wukai * * * * * * @date: 2017/9/28 * * * * @description: * * * * * * @version 1.0 * * * * * * *******************************************************/ public class ListFragment extends Fragment { RecyclerView mRecyclerView; @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { mRecyclerView= (RecyclerView) inflater.inflate(R.layout.list_test_fragment,container,false); return mRecyclerView; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); mRecyclerView.setLayoutManager(new LinearLayoutManager(mRecyclerView.getContext())); mRecyclerView.setAdapter(new RecyclerViewAdapter(mRecyclerView.getContext())); } }============================================================================================
package com.tablayoutviewpagerrecyclerview.tablayoutviewpagerrecyclerview; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentStatePagerAdapter; import java.util.List; /******************************************************* * @author: wukai * * * * * * @date: 2017/9/28 * * * * @description: * * * * * * @version 1.0 * * * * * * *******************************************************/ public class Fragement_testAdapter extends FragmentStatePagerAdapter { List<Fragment> mFragments; List<String> mTitles; public Fragement_testAdapter(FragmentManager fm, List<Fragment> fragments, List<String> titles) { super(fm); this.mFragments = fragments; this.mTitles = titles; } @Override public Fragment getItem(int position) { return mFragments.get(position); } @Override public int getCount() { return mFragments.size(); } @Override public CharSequence getPageTitle(int position) { return mTitles.get(position); } }===========================================================================================
package com.tablayoutviewpagerrecyclerview.tablayoutviewpagerrecyclerview; import android.content.Context; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Toast; /******************************************************* * @author: wukai * * * * * * @date: 2017/9/28 * * * * @description: * * * * * * @version 1.0 * * * * * * *******************************************************/ public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> { private Context mContext; public RecyclerViewAdapter(Context context) { mContext = context; } @Override public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(mContext).inflate(R.layout.list_test_recycler, parent, false); return new MyViewHolder(view); } @Override public void onBindViewHolder(MyViewHolder holder, int position) { View view = holder.mView; view.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Toast.makeText(mContext, "點選事件!!!", Toast.LENGTH_SHORT).show(); } }); } @Override public int getItemCount() { return 10; } class MyViewHolder extends RecyclerView.ViewHolder { private View mView; public MyViewHolder(View itemView) { super(itemView); mView = itemView; } } }===========================================================================================
jar包
//Material Design 設計風格的庫 compile 'com.android.support:design:26.0.0-alpha1' //recyclerview的依賴 compile 'com.android.support:recyclerview-v7:26.0.0-alpha1' compile 'com.android.support:cardview-v7:26.0.0-alpha1' compile 'com.android.support:palette-v7:26.0.0-alpha1' //懶人框架 compile 'com.jakewharton:butterknife:8.4.0' annotationProcessor 'com.jakewharton:butterknife-compiler:8.4.0' compile 'com.android.support:cardview-v7:24.0.0' compile 'de.hdodenhof:circleimageview:2.1.0'