从片段中使用适配器中的picasso获取图像

muk*_*lah 8 android android-fragments

我有一个循环视图,它使用包含textview和imageview的自定义适配器从api获取数据,我在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 {
            }
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

那么这是我的适配器:

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

}
Run Code Online (Sandbox Code Playgroud)

你可以看到这些线:

Picasso.with(mContext).load(mChannelItems.get(position).getFullImagePath()).resize(500,500).placeholder(R.drawable.placeholder).error(R.drawable.placeholder).into(holder.Image);
Run Code Online (Sandbox Code Playgroud)

和:

holder.Cat.setText(String.valueOf(mChannelItems.get(position).getTitle()));

这个猫在textview中仍然显示得很好,但是毕加索库的图像不再显示了,我读了很多解决方案,比如将片段从片段传递到适配器但没有任何效果!

这是调试图像: 在此输入图像描述

在此输入图像描述

在此输入图像描述

在此输入图像描述

Moh*_*ara 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);

}
Run Code Online (Sandbox Code Playgroud)