Tôi đã kiểm tra các câu trả lời và bài viết khác. Nhưng không thể tìm thấy những gì tôi đang tìm kiếm. Tôi có thể thiếu một cái gì đó.Tải một đoạn recyclerview khi nhấp vào mục cụ thể của một đoạn recyclerview khác bằng cách sử dụng phân đoạn tab?
Tôi có một hoạt động theo thẻ, trong đó có ba mảnh:
- FragmentHome (Tab 1),
- FragmentTwo (Tab 2),
- FragmentThree (Tab 3).
Bây giờ tôi muốn tải một Fragment khác ("FragmentAnother") khi nhấp vào Mặt hàng cụ thể của HomeFragment . Nói, tôi muốn tải "FragmentAnother" (RecyclerViewFragment) khi nhấp vào mục "5th" trong HomeFragment (Recyclerview fragment). Và tôi muốn tải chi tiết cụ thểXem cho một mục cụ thể trong HomeFragment. Làm thế nào để đạt được điều này?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
FragmentHome fragmentTabOne = new FragmentHome();
return fragmentTabOne;
case 1:
FragmentTabTwo fragmentTabTwo = new FragmentTabTwo();
return fragmentTabTwo;
case 2:
FragmentTabThree fragmentTabThree = new FragmentTabThree();
return fragmentTabThree;
default:
return null;
}
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
}
FragmentHome.java (Tab Một hoặc tab đầu tiên)
public class FragmentHome extends android.support.v4.app.Fragment {
private RecyclerView homeRecyclerView;
private RecyclerView.Adapter homeListAdapter;
private ArrayList<HomeListItem> homeListItems;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
homeRecyclerView = (RecyclerView) rootView.findViewById(R.id.homeRecyclerView);
homeRecyclerView.setHasFixedSize(true);
homeListItems = HomeListItemData.getItem();
homeListAdapter = new HomeListRecyclerViewAdapter(homeListItems, getContext());
homeRecyclerView.setAdapter(homeListAdapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
homeRecyclerView.setLayoutManager(llm);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
HomeListItem.java
public class HomeListItem {
private int CardImageId;
private String Heading;
private String Description;
public HomeListItem(int cardImageId, String heading, String description) {
this.CardImageId = cardImageId;
Heading = heading;
Description = description;
}
public String getHeading() {
return Heading;
}
public String getDescription() {
return Description;
}
public int getCardImageId() {
return CardImageId;
}
}
HomeListData.java
public class HomeListItemData {
public static ArrayList<HomeListItem> getItem() {
ArrayList<HomeListItem> homeListItemArrayList = new ArrayList<>();
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_launcher_background, "First 10 Reasons Why", "First 10 reasons accordig to"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Second 10 Reasons Why", "Second 10 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Third 14 Reasons Why", "Third 14 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Forth 5 Reasons Why", "Forth 5 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Fifth 12 Reasons Why", "Fifth 12 reasons Je suis content de savoir short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Sixth 25 Reasons Why", "Sixth 25 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Seventh 30 Reasons Why", "Seventh 30 reasons"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Eighth 8 Reasons Why", "Eighth 8 reasons Je suis content de savoir short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Ninth 34 Reasons Why", "Ninth 34 reasons Je suis content de savoir short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Tenth 9 Reasons Why", "Tenth 9 reasons Je suis content de savoir short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Eleventh 2 Reasons Why", "Eleventh 2 reasons Je suis content de savoir short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Twelve 15 Reasons Why", "Twelve 15 reasons Je suis content de savoir short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star, "Thirteenth 17 Reasons Why", "Thirteenth 17 reasons Je suis content de savoir short description"));
return homeListItemArrayList;
}
}
HomeListRecyclerViewAdapter
public class HomeListRecyclerViewAdapter extends
RecyclerView.Adapter<HomeListRecyclerViewAdapter.ViewHolder> {
List<HomeListItem> homeListItems;
Context context;
public HomeListRecyclerViewAdapter(List<HomeListItem> homeListItems, Context context) {
this.homeListItems = homeListItems;
this.context = context;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View homeListViewHolder = LayoutInflater.from(parent.getContext())
.inflate(R.layout.home_list_item, parent, false);
return new ViewHolder(homeListViewHolder);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
final HomeListItem homeListItem = homeListItems.get(position);
if (position==0){
` holder.cardImageView.setBackgroundResource(R.drawable.circle_background_green_blue_deep);
} else if (position==1)
holder.cardImageView.setBackgroundColor(Color.parseColor("#FFC107"));
} else if (position==3){
holder.cardImageView.setBackgroundColor(Color.CYAN);
}
holder.cardImageView.setImageResource(homeListItem.getCardImageId());
holder.headingTextView.setText(homeListItem.getHeading());
holder.shortDescriptionTextView.setText(homeListItem.getDescription());
//Load another fragment on Click a card
holder.homeListCardViewRelativeLayout.setOnClickListener(new
View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(context, "You clicked " + position,
Toast.LENGTH_SHORT).show();
}
});
}
@Override
public int getItemCount() {
return homeListItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView cardImageView;
public TextView headingTextView;
public TextView shortDescriptionTextView;
public RelativeLayout homeListCardViewRelativeLayout;
public ViewHolder(View itemView) {
super(itemView);
cardImageView = (ImageView)
itemView.findViewById(R.id.cardImageView);
headingTextView = (TextView)
itemView.findViewById(R.id.headingTextView);
shortDescriptionTextView = (TextView)
itemView.findViewById(R.id.shortDescriptionTextView);
homeListCardViewRelativeLayout = (RelativeLayout)
itemView.findViewById(R.id.homeListCardItemRelativeLayoutId);
}
}
}
hoạt động xml chính tập tin
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.hello.abc.activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_weight="1"
android:layout_width="match_parent"
android:background="?attr/colorPrimary"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay"
app:title="@string/app_name">
</android.support.v7.widget.Toolbar>
<View
android:id="@+id/toolbarDropShadow"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="@drawable/toolbar_drop_shadow"/>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMaxWidth="0dp"
app:tabGravity="fill"
app:tabMode="fixed">
<android.support.design.widget.TabItem
android:id="@+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Home" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab Two" />
<android.support.design.widget.TabItem
android:id="@+id/tabItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab Three" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
app:srcCompat="@android:drawable/ic_dialog_email" />
</android.support.design.widget.CoordinatorLayout>
Fragment Home xml tập tin (First Tab)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hello.abc.activity.MainActivity">
<ScrollView
android:paddingTop="9dp"
android:paddingBottom="9dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?attr/actionBarSize">
<android.support.v7.widget.RecyclerView
android:id="@+id/homeRecyclerView"
android:background="@color/backgroundColor"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
</ScrollView>
</RelativeLayout>
Trang chủ Danh sách mục xml tập tin
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
card_view:cardCornerRadius="@dimen/cardview_default_radius">
<RelativeLayout
android:id="@+id/homeListCardItemRelativeLayoutId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:foreground="?android:selectableItemBackground"
android:orientation="vertical"
android:padding="12dp">
<ImageView
android:id="@+id/cardImageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_marginRight="10dp"
android:background="@drawable/circle_bg_islam_pro_green"
android:padding="13dp"
android:src="@drawable/ic_launcher_background" />
<TextView
android:id="@+id/headingTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/cardImageView"
android:layout_marginTop="-5dp"
android:layout_toEndOf="@+id/cardImageView"
android:layout_toRightOf="@+id/cardImageView"
android:text="Heading"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Large" />
<TextView
android:id="@+id/shortDescriptionTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/headingTextView"
android:layout_toEndOf="@id/cardImageView"
android:layout_toRightOf="@+id/cardImageView"
android:text="This is short description"
android:textColor="@color/grayDark" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Bây giờ khác lớp mảnh của tôi mà tôi muốn tải về nhấp chuột mục HomeFragment nhấp chuột.
public class FragmentAnother extends Fragment {
private RecyclerView homeRecyclerView;
private RecyclerView.Adapter homeListAdapter;
private ArrayList<HomeListItem> homeListItems;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup
container, @Nullable Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
homeRecyclerView = (RecyclerView)
rootView.findViewById(R.id.homeRecyclerView);
homeRecyclerView.setHasFixedSize(true);
homeListItems = FragmentAnotherItemData.getItem();
homeListAdapter = new HomeListRecyclerViewAdapter(homeListItems,
getContext());
homeRecyclerView.setAdapter(homeListAdapter);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
homeRecyclerView.setLayoutManager(llm);
return rootView;
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
}
Một Fragment dữ liệu Lớp
public class HomeListItemData {
public static ArrayList<HomeListItem> getItem() {
ArrayList<HomeListItem> homeListItemArrayList = new ArrayList<>();
homeListItemArrayList.add(new
HomeListItem(R.drawable.ic_launcher_background, "First 4 Reasons Why",
"First 4 reasons accordig to"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Second 8 Reasons Why", "Second 8 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Third 24 Reasons Why", "Third 24 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Forth 15 Reasons Why", "Forth 15 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Fifth 10 Reasons Why", "Fifth 10 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Sixth 25 Reasons Why", "Sixth 25 reasons short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Seventh 30 Reasons Why", "Seventh 30 reasons Je suis content de savoir
short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Eighth 8 Reasons Why", "Eighth 8 reasons Je suis content de savoir short
description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Ninth 34 Reasons Why", "Ninth 34 reasons Je suis content de savoir short
description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Tenth 9 Reasons Why", "Tenth 9 reasons Je suis content de savoir short
description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Eleventh 2 Reasons Why", "Eleventh 2 reasons Je suis content de savoir
short description"));
homeListItemArrayList.add(new HomeListItem(R.drawable.ic_half_moon_and_star,
"Twelve 15 Reasons Why", "Twelve 15 reasons Je suis content de savoir short
description"));
return homeListItemArrayList;
}
}