我是android的初学者。我创建了一个recyclelerview,它解析来自JSON的数据。我已经为所有的recyclerview项目设置了一个onclick监听器,该项目打开了新活动并显示了回收站视图项目的详细信息。我的问题是,当我点击一个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);
}
}
}
});
}
}
在RecyclerView
上实现监听器而不是单个项目。这更好,因为它避免了不必要的OnClickListener
创建。
至于活动的缓慢加载,这似乎是布局优化问题。正如OğuzhanDöngül所建议的那样,您应该了解实际设备上是否存在问题。我建议阅读Improving Layout Performance然而,他们会展示许多技巧,这将有很大的帮助。