我无法创建recyclerview.OnItemClickListener,以便可以实现所有墙纸的点击。我已经使用了带有cardview的回收站视图来加载图像,并且已经成功。我是android开发和Java的初学者。如果有人可以帮助我,我将非常高兴。谢谢。
package fragments;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.example.flauntywalls.R;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.database.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
import adapters.CategoriesAdapter;
import models.Category;
/**
* Created by Belal on 4/20/2018.
*/
public class class_s10 extends Fragment {
private List<Category> categoryList;
private ProgressBar progressBar;
private DatabaseReference dbCategories;
private RecyclerView recyclerView;
private CategoriesAdapter adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.s10_frag, container, false);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
progressBar = view.findViewById(R.id.progress_bar);
progressBar.setVisibility(View.VISIBLE);
recyclerView = view.findViewById(R.id.recycler_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
categoryList = new ArrayList<>();
adapter = new CategoriesAdapter(getActivity(), categoryList);
recyclerView.setAdapter(adapter);
String category="S10";
dbCategories = FirebaseDatabase.getInstance().getReference("images").child(category);
dbCategories.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
progressBar.setVisibility(View.GONE);
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Category w= ds.getValue(Category.class);
categoryList.add(w);
}
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
以下是CategoriesAdapter的代码:
package adapters;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.example.flauntywalls.R;
import com.example.flauntywalls.activities.wallpaper_load;
import java.util.List;
import models.Category;
public class CategoriesAdapter extends RecyclerView.Adapter<CategoriesAdapter.ImageViewHolder> {
private Context mCtx;
private List<Category> imageslist;
public CategoriesAdapter(Context mCtx, List<Category> imageslist) {
this.mCtx = mCtx;
this.imageslist = imageslist;
}
@NonNull
@Override
public ImageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mCtx).inflate(R.layout.recyclerview_images,parent,false);
return new ImageViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ImageViewHolder holder, int position) {
Category images=imageslist.get(position);
Glide.with(mCtx).load(images.url).into(holder.imageView);
}
@Override
public int getItemCount() {
return imageslist.size();
}
class ImageViewHolder extends RecyclerView.ViewHolder{
ImageView imageView;
public ImageViewHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.image_view);
}
}
}
最佳答案
我建议在使用单击侦听器时使用列表视图,但是如果您需要回收视图或列表视图,则必须使用OnItemClickListener例如:
recycleview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Any action for example going to another activity
Intent intent = new Intent(Activity.this,secondActivity.class);
startActivity(intent);
}
});
如果以上内容有误,请告诉我,并发布您的日志!
关于java - 我正在制作墙纸应用程序。我被困在某个地方,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/61593981/