我有一个名为TabFragment的片段,它包含一个ImageAdapter。现在ImageAdapter将创建一个imageView,我将onclick监听器附加到它上面。单击imageView后,我想打开DetailsFragment片段。然而,在imageView上打开按下,显示Toast消息意味着访问特定片段,但片段的实际视图不是。
ImageAdapter类
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private FragmentManager fragmentManager;
public ImageAdapter(Context c,FragmentManager fm) {
mContext = c;
fragmentManager = fm;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(final int position, final View convertView, final ViewGroup parent)
{
ImageView imageView;
if(convertView == null)
{
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(300,300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
}
else
{
imageView = (ImageView) convertView;
}
imageView.setImageURI(mThumbIds[position]);
//check for tap
if(imageView.getDrawable() != null)
{
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(mContext, "clicked", Toast.LENGTH_SHORT).show();
Fragment detailsFragment = new DetailsFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.container,detailsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
}
return imageView;
}
private Uri[] mThumbIds = new Uri[20];
public void setUriArr(Uri[] uriArr){
this.mThumbIds = uriArr;
}
}
DetailsFragment Class
public class DetailsFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_details, container, false);
Toast.makeText(getContext(), "entered fragment", Toast.LENGTH_SHORT).show();
return view;
}
}
TabFragment片段 [以防万一]
public class TabFragment extends Fragment{
View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rootView = inflater.inflate(R.layout.fragment_tab, container, false);
LoadGridView(rootView);
return rootView;
}
@Override
public void onResume() {
super.onResume();
LoadGridView(rootView);
}
public void LoadGridView(View v){
GridView gridview = (GridView) v.findViewById(R.id.grdvwImages);
ImageAdapter ia = new ImageAdapter(getActivity(),getFragmentManager());
int i = 0;
Uri[] imageArray = new Uri[20];
int pos = getArguments().getInt("position", 0);
//DeleteAll();
List<GalleryModel> news = GetNewsArticles(pos);
if(news.size() != 0){
for(GalleryModel n:news){
imageArray[i] = Uri.parse(n.imageDir);
i++;
}
ia.setUriArr(imageArray);
}
gridview.setAdapter(ia);
}
public List<GalleryModel> GetNewsArticles(int position){
return new Select()
.from(GalleryModel.class)
.where("Section = ?", position)
.execute();
}
public void DeleteAll()
{
new Delete().from(GalleryModel.class).execute();
}
}
fragment_details.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.jerome_pc.myfirstapp.PortalActivity">
<RelativeLayout
android:background="@android:color/white"
android:layout_width="395dp"
android:layout_height="587dp"
tools:layout_editor_absoluteX="-5dp"
tools:layout_editor_absoluteY="-1dp">
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button"
android:layout_alignStart="@+id/button"
android:layout_below="@+id/editText2"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="Post Description"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="47dp"
android:ems="10"
android:hint="Post Title"
android:inputType="textPersonName"
android:layout_alignParentTop="true"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="200dp"
android:layout_height="200dp"
app:srcCompat="@android:drawable/ic_menu_gallery"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/editText"
android:layout_alignStart="@+id/editText" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:layout_marginStart="75dp"
android:layout_marginTop="33dp"
android:onClick="btnCancelOnClick"
android:text="Cancel"
android:layout_below="@+id/imageView3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="btnSubmitOnClick"
android:text="Submit"
android:layout_alignBaseline="@+id/button"
android:layout_alignBottom="@+id/button"
android:layout_toRightOf="@+id/button"
android:layout_toEndOf="@+id/button"
android:layout_marginLeft="51dp"
android:layout_marginStart="51dp" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
答案 0 :(得分:0)
替换而不是添加
Fragment detailsFragment = new DetailsFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container,detailsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
编辑:从relativelayout中移除绝对x和y
<RelativeLayout
android:background="@android:color/white"
android:layout_width="395dp"
android:layout_height="587dp">
并在gradle中检查您的约束布局版本
EDIT2:整理您的代码
为imageadapter中的图像创建单击侦听器,如下所示:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
private FragmentManager fragmentManager;
private ImageAdapterClick imageAdapterListener;
public interface ImageAdapterClick {
void isClicked();
}
public ImageAdapter(Context c,FragmentManager fm, ImageAdapterClick imageAdapterListener) {
mContext = c;
fragmentManager = fm;
this.imageAdapterListener = imageAdapterListener;
}
然后在getView方法中更改本地侦听器
imageView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
imageAdapterListener.isClicked();
}
});
然后在TabFragment中以调用该详细信息片段的方式更改适配器
ImageAdapter ia = new ImageAdapter(getActivity(),getFragmentManager(), new
ImageAdapter.ImageAdapterClick (){
@Override
public void isClicked() {
Fragment detailsFragment = new DetailsFragment();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container,detailsFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});