活动加载时间过多

时间:2018-09-16 04:27:24

标签: android android-studio onitemclicklistener

我是android的初学者。我创建了一个recyclerview来解析JSON中的数据。我已经为所有recyclerview项设置了onclick侦听器,这将打开新活动并显示recycler视图项的详细信息。我的问题是,当我单击一个recyclerview项时,加载它需要4-5秒,我可以加快速度吗,请帮忙。

我的活动代码

public class Tab1 extends Fragment implements ExampleAdapter.onItemClickListener{
    private RecyclerViewPager mRecyclerView;
    private ExampleAdapter mExampleAdapter;
    private ArrayList<ExampleItem> mExampleList;
    private RequestQueue mRequestQueue;
    public static final String EXTRA_URL = "imageUrl";
    private ShimmerFrameLayout mShimmerViewContainer;
    Button sharebuttonz;
    File imagePathz;
    @Nullable
    @Override

    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
        View rootView = inflater.inflate(R.layout.tab1, container, false);
        mRecyclerView = rootView.findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mShimmerViewContainer = rootView.findViewById(R.id.shimmer_view_container);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        mExampleList = new ArrayList<>();
        mRequestQueue = Volley.newRequestQueue(getActivity().getApplicationContext());


        // Set the adapter here.
        // Use getActivity() instead of getContext()
        parseJson();
        mExampleAdapter = new ExampleAdapter(getActivity(), mExampleList);
        mRecyclerView.setAdapter(mExampleAdapter);
        mExampleAdapter.setOnItemClickListener(Tab1.this);


      return rootView;
    }



    private void parseJson() {
        String url = "http://maranamassapp.cf/json_getdata.php";
        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("server_response");
                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject ser = jsonArray.getJSONObject(i);
                                String creatorname = ser.getString("head");
                                String imageUrl = ser.getString("image");
                                String cat = ser.getString("content");
                                String postdate = ser.getString("weburl");
                                String dateall = ser.getString("date");
                                int type = ser.getInt("type");

                                mExampleList.add(new ExampleItem(imageUrl, creatorname, cat, postdate, dateall,type));
                            }



                            // Just call notifyDataSetChanged here
                            mExampleAdapter.notifyDataSetChanged();
                            mShimmerViewContainer.stopShimmerAnimation();
                            mShimmerViewContainer.setVisibility(View.GONE);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }


        public void onItemClick(int position) {
            Intent detailintent = new Intent(getContext(), DetailActivity.class);
            ExampleItem clickeditem = mExampleList.get(position);
            detailintent.putExtra(EXTRA_URL, clickeditem.getmDate());
            startActivity(detailintent);
    }
}

我的适配器代码

public class ExampleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private ArrayList<ExampleItem> mExampleList;
    private onItemClickListener mListener;


    public interface onItemClickListener {
        void onItemClick(int position);
    }

    public void setOnItemClickListener(onItemClickListener listener) {
        mListener = listener;
    }

    public class ExampleViewHolder extends RecyclerView.ViewHolder {
        public ImageView mImageView;
        public TextView mTextViewCreator;
        public TextView mTextViewCat;
        public TextView mDateAll;

        public ExampleViewHolder(View itemView) {
            super(itemView);
            mImageView = itemView.findViewById(R.id.image_view);
            mTextViewCreator = itemView.findViewById(R.id.text_view_creator);
            mTextViewCat = itemView.findViewById(R.id.text_view_cat);
            mDateAll = itemView.findViewById(R.id.date_views);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mListener != null) {
                        int position = getAdapterPosition();
                        if (position != RecyclerView.NO_POSITION) {
                            mListener.onItemClick(position);
                        }
                    }
                }
            });
        }
    }

1 个答案:

答案 0 :(得分:0)

RecyclerView上实施侦听器,而不是单个项目。这样会更好,因为它可以避免不必要的OnClickListener创建。

对于缓慢的活动加载,这似乎是布局优化问题。根据OğuzhanDöngül的建议,您应该找出实际设备上是否仍然存在问题。我还是建议您阅读Improving Layout Performance,它们显示了很多技巧,将大有帮助。