我有一个recycleview,它使用包含textview和imageview的自定义适配器从api中获取数据,我在Activity中实现了这一点,并且效果很好,但是现在我尝试使用fragment而不是activity,除了将代码移动到片段后未显示的图像,这是我的片段:
public class TVFragment extends Fragment implements TVAdapter.TVAdapterOnClickHandler {
TVAdapter mAdapter;
RecyclerView mSportsList;
String sortsports="sports.php";
Context mContext ;
public static TVFragment newInstance() {
TVFragment fragment = new TVFragment();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_tv, container, false);
mSportsList = (RecyclerView) view.findViewById(R.id.rv_sports);
mContext = getActivity();
GridLayoutManager LayoutManagerSports = new GridLayoutManager(getActivity(), 3);
mSportsList.setLayoutManager(LayoutManagerSports);
mSportsList.setHasFixedSize(true);
mAdapter = new TVAdapter(this, mContext);
mSportsList.setAdapter(mAdapter);
loadTVData(sortsports);
return view;
}
private void loadTVData(String sortChannels) {
showTVDataView();
new FetchTVTask().execute(sortChannels);
}
@Override
public void onClick(TVItem channel) {
Context context = getActivity().getApplicationContext();
Class destinationClass = TVChannel.class;
Intent intentToStartDetailActivity = new Intent(context, destinationClass);
intentToStartDetailActivity.putExtra("TVChannel", channel);
startActivity(intentToStartDetailActivity);
}
private void showTVDataView() {
mSportsList.setVisibility(View.VISIBLE);
}
public class FetchTVTask extends AsyncTask<String, Void, ArrayList<TVItem>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected ArrayList<TVItem> doInBackground(String... params) {
if (params.length == 0) {
return null;
}
String sortChannels = params[0];
URL channelRequestUrl = NetworkTV.buildUrl(sortChannels);
try {
String jsonTVResponse = NetworkTV.getResponseFromHttpUrl(channelRequestUrl);
ArrayList<TVItem> simpleJsonTVData = JsonTV.getSimpleTVStringsFromJson(getContext(), jsonTVResponse);
return simpleJsonTVData;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@Override
protected void onPostExecute(ArrayList<TVItem> TVData) {
if (TVData != null) {
showTVDataView();
mAdapter.setTVData(TVData);
} else {
}
}
}
}
然后这是我的适配器:
public class TVAdapter extends RecyclerView.Adapter<TVAdapter.RecyclerViewHolder> {
ArrayList<TVItem> mChannelItems;
private Context mContext;
private final TVAdapter.TVAdapterOnClickHandler mClickHandler;
public interface TVAdapterOnClickHandler {
void onClick(TVItem channel);
}
public TVAdapter(TVAdapterOnClickHandler clickHandler, Context context) {
this.mContext = context;
mClickHandler = clickHandler;
}
class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public final TextView Cat;
public final ImageView Image;
public RecyclerViewHolder(View view) {
super(view);
Cat = (TextView)itemView.findViewById(R.id.channel_cat);
Image = (ImageView) itemView.findViewById(R.id.channel_image);
view.setOnClickListener(this);
}
@Override
public void onClick(View v) {
int adapterPosition = getAdapterPosition();
TVItem channel = mChannelItems.get(adapterPosition);
mClickHandler.onClick(channel);
}
}
@Override
public TVAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
mContext = viewGroup.getContext();
int layoutIdForListItem = R.layout.tv_list_item;
LayoutInflater inflater = LayoutInflater.from(mContext);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
return new TVAdapter.RecyclerViewHolder(view);
}
@Override
public void onBindViewHolder(TVAdapter.RecyclerViewHolder holder, int position) {
Picasso.with(mContext).load(mChannelItems.get(position).getFullImagePath()).resize(500,500).placeholder(R.drawable.placeholder).error(R.drawable.placeholder).into(holder.Image);
holder.Cat.setText(String.valueOf(mChannelItems.get(position).getTitle()));
}
@Override
public int getItemCount() {
if (null == mChannelItems)
return 0;
else {
return mChannelItems.size();
}
}
public void setTVData(ArrayList<TVItem> TVData) {
mChannelItems = TVData;
notifyDataSetChanged();
}
}
您可以看到以下几行:
Picasso.with(mContext).load(mChannelItems.get(position).getFullImagePath()).resize(500,500).placeholder(R.drawable.placeholder).error(R.drawable.placeholder).into(holder.Image);
和:
holder.Cat.setText(String.valueOf(mChannelItems.get(position).getTitle()));
这只猫在textview中仍然显示良好,但是带有毕加索库的图像不再显示,我读了很多解决方案,例如将上下文从片段传递到适配器,但无济于事!
答案 0 :(得分:1)
viewgroup.getContext()不是所需的上下文,而是片段或活动中的上下文。
@Override
public TVAdapter.RecyclerViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
//mContext = viewGroup.getContext(); This should fix it. You already init mContext in the constructor.
int layoutIdForListItem = R.layout.tv_list_item;
LayoutInflater inflater = LayoutInflater.from(mContext);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
return new TVAdapter.RecyclerViewHolder(view);
}
答案 1 :(得分:1)
为什么要两次为mContext
赋值?尝试从代码中删除mContext = viewGroup.getContext();
。