2017-01-30 58 views
0

我想在我的app.i中使用列表(recyclerView或列表視圖)在我的應用程序中使用了ahBottom導航,我想在顯示列表視圖時單擊其中的一個。 請幫助我。如何在片段中使用recyclerView或列表視圖

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; 
} 
+0

可能的重複:http://stackoverflow.com/questions/26621060/display-a-recyclerview-in-fragment –

回答