如何在片段中使用recyclelerView或列表视图

时间:2017-01-30 07:33:11

标签: android android-studio android-recyclerview fragment

我想在我的app.i中使用列表(recyclerView或列表视图)在我的应用程序中使用了ahBottom导航,我想在单击其中一个时显示列表视图。 请帮帮我。

mainactivity.java:

public class MainActivity extends AppCompatActivity {

private ArrayList<String> list_poems;
private AHBottomNavigation ahBottomNavigation;
private ArrayList<AHBottomNavigationItem> ahBottomNavigationItemArrayList = new ArrayList<>();
private AHBottomNavigationViewPager viewPager;
private ViewpageAdapter adapter;
private ArrayAdapter adapter_lists;
private Database db;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    list_poems = db.list_title();
    adapter_lists = new ArrayAdapter(this, android.R.layout.simple_list_item_1, list_poems);

    CoordinatorLayout coordinatorLayout = (CoordinatorLayout) findViewById(R.id.text);



    ahBottomNavigation = (AHBottomNavigation) findViewById(R.id.Ahbottom);
    ahBottomNavigation.setColored(false);
    ahBottomNavigation.setBackgroundColor(Color.BLUE);
    viewPager = (AHBottomNavigationViewPager) findViewById(R.id.ViewPager);

    AHBottomNavigationItem item1 = new AHBottomNavigationItem("اشعار", ContextCompat.getDrawable(this, R.drawable.ic_format_list_bulleted_black_24dp), Color.parseColor("#f2ff50"));
    AHBottomNavigationItem item2 = new AHBottomNavigationItem("جستجو", ContextCompat.getDrawable(this, R.drawable.ic_search_black_24dp), Color.parseColor("#f2ff50"));
    AHBottomNavigationItem item3 = new AHBottomNavigationItem("تنظیمات", ContextCompat.getDrawable(this, R.drawable.ic_settings_black_24dp), Color.parseColor("#f2ff50"));
    ahBottomNavigationItemArrayList.add(item1);
    ahBottomNavigationItemArrayList.add(item2);
    ahBottomNavigationItemArrayList.add(item3);

    ahBottomNavigation.addItems(ahBottomNavigationItemArrayList);

    ahBottomNavigation.setAccentColor(Color.parseColor("#53D9FF"));
    ahBottomNavigation.setInactiveColor(Color.parseColor("#000000"));

    ahBottomNavigation.setOnTabSelectedListener(new AHBottomNavigation.OnTabSelectedListener() {
        @Override
        public void onTabSelected(int position, boolean wasSelected) {
            viewPager.setCurrentItem(position);

            Fragment fragment = new Fragment();
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.frm, fragment).commit();
        }
    });

    viewPager.setOffscreenPageLimit(4);
    adapter = new ViewpageAdapter(getSupportFragmentManager());
    viewPager.setAdapter(adapter);
}

viewpageradapter:

public class ViewpageAdapter extends FragmentPagerAdapter {
private ArrayList<Fragment_instance> array_frag = new ArrayList<>();

public ViewpageAdapter(FragmentManager fm){
    super(fm);
    array_frag.add(Fragment_instance.newInstance("علاقه"));
    array_frag.add(Fragment_instance.newInstance("جستجو"));
    array_frag.add(Fragment_instance.newInstance("تنظیمات"));
}
@Override
public Fragment_instance getItem(int position) {
    return array_frag.get(position);
}

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

Fragment_instance:

public class Fragment_instance extends Fragment {

private static String frag_key = "fragment title";

public static Fragment_instance newInstance(String title) {
    Fragment_instance fragmentInstance = new Fragment_instance();
    Bundle args = new Bundle();
    args.putString(frag_key, title);
    fragmentInstance.setArguments(args);
    return fragmentInstance;
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment, container, false);

    return view;
}

1 个答案:

答案 0 :(得分:0)

我认为他的演示项目非常适合学习。您可以尝试设置和探索。 https://github.com/aurelhubert/ahbottomnavigation