gpt4 book ai didi

Android GridView(自定义) fragment 图片,但是打不开页面

转载 作者:行者123 更新时间:2023-11-29 23:32:06 25 4
gpt4 key购买 nike

我想在Fragement中做一个自定义的GridView,使用 URLHTTP 获取 url 并使用 Bitmap

但我只完成了一半,

我无法打开 MapFragement 页面并且找不到我的bug 很长时间都解决不了问题,请帮帮我。

请帮助或尝试提供一些想法如何实现这一目标,

谢谢!

我的代码:

item_gridview_member.xml

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/imgmember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>

fragement_member.xml

       <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MemberFragment">
<GridView
android:id="@+id/gridmember"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="150dp"
android:stretchMode="columnWidth"
android:numColumns="3"
android:verticalSpacing="3dp"
android:layout_marginBottom="3dp">
</GridView>

CImage.java

    import android.graphics.Bitmap;

public class CImage {

private String imagePath;
private Bitmap bitmap;

public CImage(String imagePath, Bitmap bitmap) {
this.imagePath = imagePath;
this.bitmap = bitmap;
}

public String getImagePath() {
return imagePath;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}

}

GridViewAdapter.java

    import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import com.example.yao.pm.R;
import java.util.List;
public class GridViewAdapter extends BaseAdapter {

private LayoutInflater myInflater;

private List<CImage> ImgList;

public GridViewAdapter(Context context,List<CImage> ImgList) {
myInflater = LayoutInflater.from(context);
this.ImgList = ImgList;
}


@Override
public int getCount() {
return ImgList.size();
}

@Override
public Object getItem(int position) {
return ImgList.get(position);
}

@Override
public long getItemId(int position) {
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;


if(convertView == null)
{
convertView = myInflater.inflate(R.layout.item_gridview_member,null);
holder = new ViewHolder();
holder.imageView = convertView.findViewById(R.id.image_meme);
convertView.setTag(holder);
}

CImage image = ImgList.get(position);

holder.imageView.setImageBitmap(image.getBitmap());

if (image.getBitmap() == null) {
holder.imageView.setImageResource(R.mipmap.ic_launcher);
} else {
holder.imageView.setImageBitmap(image.getBitmap());
}

return convertView;
}

public class ViewHolder {
ImageView imageView;
}

}

MapFregement.java

    public class MemberFragment extends Fragment {


private GridViewAdapter adapter;

private ArrayList<CImage> imageList;

public MemberFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_member, container, false);

}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

inital();
adapter = new GridViewAdapter(getActivity(),imageList);
grieview.setAdapter(adapter);

}

public void inital(){

grieview = getActivity().findViewById(R.id.gridmember);

}
GridView grieview;
}

谢谢!!

最佳答案

查看代码后,我认为问题出在 MemberFragment 类中的 GridView

您正在使用 inflater 将 View 膨胀为 fragment ,并尝试使用 Activity 上下文解析 id。此外,您需要覆盖 Fragment 中的 onViewCreated()

What is usage of onViewCreated()?

Called immediately after onCreateView(LayoutInflater, ViewGroup,
Bundle)
has returned, but before any saved state has been restored in to the view. This gives subclasses a chance to initialize themselves once they know their view hierarchy has been completely created. The fragment's view hierarchy is not however attached to its parent at this point.


您的MemberFragment.java:

public class MemberFragment extends Fragment {

private GridView grieview;
private GridViewAdapter adapter;
private ArrayList<CImage> imageList;

public MemberFragment() {
// Required empty public constructor
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_member, container, false);

}

//Note: Change is from here, Instead of onActivityCreated(), override this method. Use View object from it.
@Override
void onViewCreated (View view, Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
inital(view); // Get the View Object provided from method parameter to find your fragment layout view IDs, It's the same View you return in onCreateView method.
adapter = new GridViewAdapter(getActivity(),imageList);
grieview.setAdapter(adapter);
}

public void inital(View view){
grieview = view.findViewById(R.id.gridmember); //Find your view inside fragment like this.
}

}

希望对你有帮助!

关于Android GridView(自定义) fragment 图片,但是打不开页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52542372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com