这是我的活动类,它有回收视图。现在我可以点击循环视图中的项目,它将打开另一个类。当我点击它时,我有一个按钮,我希望它打开课程。所以基本上重复当我按下项目而不是按钮时会发生什么。当我滚动时,我尝试使用此int position3 = recyclerView.getChildLayoutPosition(v);
更改项目的位置,以便我可以根据项目本身打开活动类,但是我收到此错误。
android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams`。
FirebaseDatabase firebaseDatabaseInstance;
private DatabaseReference booksInstance;
AlbumDBHandler db;
ArrayList<BookData> books = new ArrayList<>();
private String TAG = mainActivityCarasoul.class.getSimpleName();
private GalleryAdapter mAdapter;
private RecyclerView recyclerView;
private Button button;
private int position2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_carasoul);
final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL, true);
layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener2());
recyclerView = (RecyclerView) findViewById(R.id.recycler_view_carasoul);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
button = (Button) findViewById(R.id.main_activity_button_carasoul);
button.setOnClickListener(this);
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener()
{
@Override
public void onClick(View view, int position) {
position2 = recyclerView.getChildLayoutPosition(view);
mAdapter.notifyDataSetChanged();
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(position));
intent.putExtra("from", "mainActivityCarasoul");
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
recyclerView.addOnScrollListener(new CenterScrollListener());
db = new AlbumDBHandler(this);
getData();
}
private void getData(){
firebaseDatabaseInstance = FirebaseDatabase.getInstance();
// get reference to 'users' node
booksInstance = firebaseDatabaseInstance.getReference("mo2lfat");
books.clear();
books.addAll(db.getAllBook());
mAdapter = new GalleryAdapter(getApplicationContext(), books);
recyclerView.setAdapter(mAdapter);
if (books.isEmpty()) {
}
booksInstance.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
storeData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
private void storeData(DataSnapshot snapshot) {
books.clear();
for (DataSnapshot alert: snapshot.getChildren()) {
BookData book = new BookData(
(String)alert.child("id").getValue(),
(String)alert.child("book_name").getValue(),
(String)alert.child("book_path").getValue(),
(String)alert.child("book_path").getValue(),
"",
(String)alert.child("image_path").getValue(),
(String)alert.child("image_path").getValue(),
""
);
db.insertBook(book);
}
books.addAll(db.getAllBook());
mAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
// int position3 = recyclerView.getChildLayoutPosition(v);
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(position2));
intent.putExtra("from", "mainActivityCarasoul");
startActivity(intent);
}
这是我的适配器类
Activity activity;
ArrayList<BookData> images = new ArrayList<>();
AlbumDBHandler db;
private Context mContext;
public class MyViewHolder extends RecyclerView.ViewHolder {
public ImageView thumbnail;
public TextView name;
public Button button;
public MyViewHolder(View view) {
super(view);
thumbnail = (ImageView) view.findViewById(R.id.thumbnail);
name = (TextView) view.findViewById(R.id.textViewGallery);
}
}
public GalleryAdapter(Context context, ArrayList<BookData> images) {
mContext = context;
this.images = images;
this.db = new AlbumDBHandler(context);
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.gallery_thumbnail, parent, false);
return new MyViewHolder(itemView);
}
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
BookData image = images.get(position);
if("".equals(images.get(position).getImageLocalPath())){
Glide.with(mContext)
.load(images.get(position).getImagePath_2())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.thumbnail);
new ImageDownloaderTask(images.get(position)).execute(images.get(position).getImagePath_2());
}else{
Glide.with(mContext).load(new File(images.get(position).getImageLocalPath())).into(holder.thumbnail);
}
holder.name.setText(images.get(position).getName());
}
public static class RecyclerTouchListener implements RecyclerView.OnItemTouchListener {
private GestureDetector gestureDetector;
private GalleryAdapter.ClickListener clickListener;
public RecyclerTouchListener(Context context, final RecyclerView recyclerView, final GalleryAdapter.ClickListener clickListener) {
this.clickListener = clickListener;
gestureDetector = new GestureDetector(context, new GestureDetector.SimpleOnGestureListener() {
@Override
public boolean onSingleTapUp(MotionEvent e) {
return true;
}
@Override
public void onLongPress(MotionEvent e) {
View child = recyclerView.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null) {
clickListener.onLongClick(child, recyclerView.getChildPosition(child));
}
}
});
}
@Override
public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
View child = rv.findChildViewUnder(e.getX(), e.getY());
if (child != null && clickListener != null && gestureDetector.onTouchEvent(e)) {
clickListener.onClick(child, rv.getChildPosition(child));
}
return false;
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e) {
}
@Override
public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
}
}
答案 0 :(得分:1)
#如果您的RecylcerView
项目中包含Button
且您希望通过点击Activity
更改Button
,那么您可以尝试这样:
在您的适配器onBindViewHolder()
中,设置按钮onClick
听众:
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
BookData image = images.get(position);
.............
.................
viewHolder.button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// Do somethings
}
});
}
#如果您的MainActivityCarasoul
有一个Button
而您希望通过点击Activity
更改Button
,那么您可以尝试以下操作:
在MainActivityCarasoul
中,执行以下更改:
...........
.................
private int mPosition = 0; // Initialize
@Override
protected void onCreate(Bundle savedInstanceState) {
........
................
button = (Button) findViewById(R.id.main_activity_button_carasoul);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// Do somethings
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(mPosition));
intent.putExtra("from", "mainActivityCarasoul");
startActivity(intent);
}
});
}
<强>更新强>
更新position
试试这个:
recyclerView.addOnItemTouchListener(new
GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener()
{
@Override
public void onClick(View view, int position) {
mPosition = position; // update
..............
....................
}));
希望这会有所帮助〜
答案 1 :(得分:0)
有两种方法可以做到这一点。
一个是将onclicklistener设置为回收器适配器recylers视图项视图并在那里执行您的逻辑。在那里,您可以通过viewholder.getadapterposition
访问该位置其他方法是,如果您将click侦听器设置为recycler视图的itemtouchlistener,那么您得到的参数就是位置。你再次尝试通过调用getchildposition来获取位置
出现此错误是因为项目的父项具有linearlayout,因此传递给getchilfpositiom的视图具有linearlayout参数,这会导致无法转换为作为父项的recyclerview参数。
最好尝试使用你得到的位置。
如果您需要获取相应的适配器项,请使用一种方法来修改适配器,该方法可以调整位置并返回相应的项目。
Adapter.getItemAtPosition(位置)
如果您想获得相应的项目视图。 Recyclerview.getviewholder会有一些方法。
尝试使用回收器视图的内置函数,而不是使用视图方法。
答案 2 :(得分:0)
以下行正在抛出你提到的异常。
position2 = recyclerView.getChildLayoutPosition(view);
在这里你不需要这一行,你可以使用以下代替上述行。
position2 = position ;
我修改了你的代码,我上面提到的一个改变,还有两个你需要做的改变
1。注释notifyDataSetChanged()就在下方 getChildLayoutPosition(视图)
2。在notifyDataSetChanged()之前设置书籍。
以下是修改后的代码:
FirebaseDatabase firebaseDatabaseInstance;
private DatabaseReference booksInstance;
AlbumDBHandler db;
ArrayList<BookData> books = new ArrayList<>();
private String TAG = mainActivityCarasoul.class.getSimpleName();
private GalleryAdapter mAdapter;
private RecyclerView recyclerView;
private Button button;
private int position2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_carasoul);
final CarouselLayoutManager layoutManager = new CarouselLayoutManager(CarouselLayoutManager.HORIZONTAL, true);
layoutManager.setPostLayoutListener(new CarouselZoomPostLayoutListener2());
recyclerView = (RecyclerView) findViewById(R.id.recycler_view_carasoul);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
button = (Button) findViewById(R.id.main_activity_button_carasoul);
button.setOnClickListener(this);
recyclerView.addOnItemTouchListener(new GalleryAdapter.RecyclerTouchListener(this, recyclerView, new GalleryAdapter.ClickListener()
{
@Override
public void onClick(View view, int position) {
//position2 = recyclerView.getChildLayoutPosition(view);
//mAdapter.notifyDataSetChanged();
position2 = position ;
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(position));
intent.putExtra("from", "mainActivityCarasoul");
startActivity(intent);
}
@Override
public void onLongClick(View view, int position) {
}
}));
recyclerView.addOnScrollListener(new CenterScrollListener());
db = new AlbumDBHandler(this);
getData();
}
private void getData(){
firebaseDatabaseInstance = FirebaseDatabase.getInstance();
// get reference to 'users' node
booksInstance = firebaseDatabaseInstance.getReference("mo2lfat");
books.clear();
books.addAll(db.getAllBook());
mAdapter = new GalleryAdapter(getApplicationContext(), books);
recyclerView.setAdapter(mAdapter);
if (books.isEmpty()) {
}
booksInstance.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
storeData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
}
private void storeData(DataSnapshot snapshot) {
books.clear();
for (DataSnapshot alert: snapshot.getChildren()) {
BookData book = new BookData(
(String)alert.child("id").getValue(),
(String)alert.child("book_name").getValue(),
(String)alert.child("book_path").getValue(),
(String)alert.child("book_path").getValue(),
"",
(String)alert.child("image_path").getValue(),
(String)alert.child("image_path").getValue(),
""
);
db.insertBook(book);
}
books.addAll(db.getAllBook());
mAdapter.setBooks(books);
mAdapter.notifyDataSetChanged();
}
@Override
public void onClick(View v) {
// int position3 = recyclerView.getChildLayoutPosition(v);
Intent intent = new Intent(mainActivityCarasoul.this, PDFViewerActivity.class);
intent.putExtra(PDFViewerActivity.TAG, books.get(position2));
intent.putExtra("from", "mainActivityCarasoul");
startActivity(intent);
}
现在在Adapter类中进行以下更改。
1。添加以下方法
public void setBooks(ArrayList<BookData> books){
this.images = books ;
}
现在构建您的代码并检查问题。
答案 3 :(得分:0)
有一个简单的方法可以做到这一点
itemView 是我的 ViewHolder
private Context context;
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context = itemView.getContext();
Intent intent = new Intent(context, yourclss.class);
context.startActivity(intent);
}
});