使用选项卡式片段单击另一个回收器视图片段的特定项目单击时加载回收器视图片段?



我已经检查了其他答案和文章。但找不到我要找的东西。我可能错过了什么。

我有一个选项卡式活动,其中包含三个片段:

  1. FragmentHome(表1)
  2. 片段二(表2)
  3. 片段三(表3)

现在我想点击HomeFragment的加载另一个Fragment("FragmentAnother")特定项目。比如说,我想在点击HomeFragment(Recyclerview fragment)中的"第五个"项目时加载"FragmentAnother"(RecycleViewFragment。我想为HomeFragment中的特定项目加载一个特定的detailView。如何做到这一点?

主要活动.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(选项卡一或第一个选项卡)

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);
}

}

主页列表项.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;
}
}

主页列表数据.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.setBackgroundResource(R.drawable.circle_background_purple__ middle_islam_pro); } else if (position==2){

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);
}
}
}

活动主xml文件

<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>

片段主页xml文件(第一个选项卡)

<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>

主页列表项xml文件

<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>

现在我的另一个片段类,我想在点击HomeFragment项点击时加载。

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);
}
}

另一个片段数据类

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

如果我答对了问题。

你有没有试过在"FragmentHome.java"中添加一个片段,如下所示?在Recyclerview适配器中显示Toast时使用此代码。

//Load another fragment on Click a card
holder.homeListCardViewRelativeLayout.setOnClickListener(new 
View.OnClickListener() {
@Override
public void onClick(View view) {
FragmentTransaction fragmentTransaction=getChildFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.relativeLayout,new DetailFragment());
fragmentTransaction.commit();
}
});

上面的代码将在您的"FragmentHome.java"中添加一个片段更多详细信息,请参阅此处。

如果您想将数据从一个片段传输到另一个片段,请参阅此处

最新更新