我已在导航抽屉中成功实施cardview and recycleview。现在的问题是,当我点击我的第一张卡片视图时,我会点击我的第一张片段,然后按返回,我的卡片视图不再出现在我的列表标签(第一张片段)中。查看cardviews列表的唯一选项是再次滑动到我的导航抽屉,然后单击第一个片段以查看cardviews。
这是我的代码 的 RecyclerViewAdapter.class
package com.example.guitarista.citem.Exhibitor;
import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.CardView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.guitarista.citem.Gallery_SpringForward.GalleryFragment;
import com.example.guitarista.citem.Gallery_activity.GalleryActivity;
import com.example.guitarista.citem.R;
import java.util.List;
/**
* Created by smdojt on 1/24/2017.
*/
public class TabInt_RV_Adapter extends RecyclerView.Adapter<TabInt_RV_Adapter.ItemViewHolder> {
public static class ItemViewHolder extends RecyclerView.ViewHolder {
static CardView cv;
TextView itemName;
TextView itemCathegory;
ImageView itemPhoto;
ItemViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
itemName = (TextView) itemView.findViewById(R.id.item_name);
itemCathegory = (TextView) itemView.findViewById(R.id.item_cathegory);
itemPhoto = (ImageView) itemView.findViewById(R.id.item_photo);
}
}
List<Exhibitors_Int> items;
Context context;
TabInt_RV_Adapter(List<Exhibitors_Int> items, Context context) {
this.context = context;
this.items = items;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public ItemViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_exhibitor_tabint_item, viewGroup, false);
ItemViewHolder ivh = new ItemViewHolder(v);
return ivh;
}
@Override
public void onBindViewHolder(ItemViewHolder itemViewHolder, final int i) {
itemViewHolder.cv.setOnClickListener(null);
itemViewHolder.itemName.setText(items.get(i).name);
itemViewHolder.itemCathegory.setText(items.get(i).cathegory);
itemViewHolder.itemPhoto.setImageResource(items.get(i).photoId);
ItemViewHolder.cv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (i == 1) {
//Toast.makeText(context, "Index position is 1", Toast.LENGTH_SHORT).show();
Intent intent = new Intent (context, GalleryActivity.class);
context.startActivity(intent);
}
else if (i == 2) {
//Toast.makeText(context, "Index position is 2 ", Toast.LENGTH_SHORT).show();
AppCompatActivity activity = (AppCompatActivity) v.getContext();
GalleryFragment gf = new GalleryFragment();
activity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, gf).addToBackStack(null).commit();
}
}
});
}
@Override
public int getItemCount() {
return items.size();
}
}
请注意,在我的onClick上,有两个版本的图库。第一个是原始活动,第二个是片段实现。
因此,当我点击第一个,然后再按下时,将显示cardviews。现在问题是什么?当我点击图库的活动版本时,我无法在导航抽屉中滑动。
在哪里,当我点击实施片段的第二个cardview时,导航抽屉滑动在图库内部工作,但是当我按下时,卡片视图列表不会显示。
即使我将这些代码行放入片段:
GalleryFragment fr = new GalleryFragment();
Bundle args = new Bundle();
fr.setArguments(args);
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fr);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
我甚至在 getFragmentManager();
中出错还有其他方法可以解决这个问题吗?
CARDVIEWS代码
package com.example.guitarista.citem.Exhibitor;
import android.os.Bundle;
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;
import com.example.guitarista.citem.R;
import java.util.ArrayList;
import java.util.List;
/**
* A simple {@link Fragment} subclass.
*/
public class TabInternationalFragment extends Fragment {
private List<Exhibitors_Int> items;
private RecyclerView rv;
public TabInternationalFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_exhibitor_tab_international, container, false);
rv=(RecyclerView)v.findViewById(R.id.rv);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
rv.setLayoutManager(llm);
//GridLayoutManager glm = new GridLayoutManager(getActivity(), 4);
//rv.setLayoutManager(glm);
rv.setHasFixedSize(true);
initializeData();
initializeAdapter();
return v;
}
private void initializeData() {
items = new ArrayList<>();
items.add(new Exhibitors_Int("A. GARCIA CRAFTS", "FURNITURE", R.drawable.image1));
items.add(new Exhibitors_Int("BALEX BOXES", "HOLIDAY DECORATION", R.drawable.image2));
items.add(new Exhibitors_Int("CAGAYAN DE ORO HANDMADE PAPER", "HOME DECOR/HOUSEWARE", R.drawable.image3));
}
private void initializeAdapter(){
TabInt_RV_Adapter adapter = new TabInt_RV_Adapter(items, getContext());
rv.setAdapter(adapter);
}
}
答案 0 :(得分:0)
如果您希望按下后退按钮后保持第一个活动,则应在AndroidManifest.xml中定义第一个活动的启动模式。还要为您的第二个活动定义父活动,以便Android知道当您按下后退按钮时它应该导航回哪个活动。
<activity
android:name=".activity.HomeActivity"
android:label="@string/app_name"
android:launchMode="singleTop" />
<activity
android:name=".activity.GalleryActivity"
android:label="@string/app_name"
android:parentActivityName=".activity.HomeActivity"
tools:ignore="UnusedAttribute">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".activity.HomeActivity" />
</activity>
如果活动堆栈中已有实例,则singleTop启动模式将确保不会创建该活动的新实例。因此,按下后退按钮后,您的卡片仍会在第一个活动中显示。