I want to go into a new Activity with the onClick() method but my code is not working. Can you please offer some advice. I have some issues with the recyclerView, since it's fairly new for me. Thank you
public class FragmentOne extends Fragment {
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
RecyclerView recyclerView = (RecyclerView) inflater.inflate(
R.layout.recycleview, container, false);
ContentAdapter adapter = new ContentAdapter();
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return recyclerView;
}
public class ContentAdapter extends RecyclerView.Adapter<ContentAdapter.ViewHolder> {
private static final int LENGTH = 50;
public ContentAdapter() {
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.fragment_channel, parent, false));
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), ChannelDetailActivity.class);
startActivity(intent);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
ViewHolder viewHolder = new ViewHolder (LayoutInflater.from(parent.getContext()), parent);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return LENGTH;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/my_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:scrollbars="vertical"
android:elevation="5dp"
android:fadingEdgeLength="@dimen/cardview_compat_inset_shadow"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="360dp"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_marginBottom="1dp"
android:elevation="2dp"
android:layout_gravity="center_horizontal"
android:background="#FFFF">
<ImageView
android:id="@+id/user_image"
android:layout_width="60dp"
android:layout_height="60dp"
android:scaleType="centerCrop"
android:src="@drawable/userone" />
<TextView
android:id="@+id/user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/user_image"
android:text="Chuck Reich"
android:paddingBottom="3dp"
android:textAppearance="?attr/textAppearanceListItem"
android:textSize="20sp" />
<TextView
android:id="@+id/user_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/user_name"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/user_image"
android:textColor="#a9a9a9"
android:ellipsize="end"
android:singleLine="true"
android:text="Horizon Media Studios"
android:textAppearance="?attr/textAppearanceListItem"
android:paddingBottom="2dp"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="3928"
android:paddingRight="18dp"
android:id="@+id/textView2"
android:textColor="#a9a9a9"
android:textSize="12sp"
android:layout_below="@+id/user_desc"
android:layout_alignStart="@+id/user_desc" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="3402"
android:id="@+id/textView3"
android:textColor="#a9a9a9"
android:textSize="12sp"
android:layout_below="@+id/user_desc"
android:layout_toEndOf="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="2354"
android:paddingLeft="20dp"
android:id="@+id/textView4"
android:textColor="#a9a9a9"
android:textSize="12sp"
android:layout_below="@+id/user_desc"
android:layout_centerHorizontal="true" />
</RelativeLayout>
I found the solution!:) There's this way of handling item click in Recyclerview with itemView given within the ViewHolder class:
public static class ViewHolder extends RecyclerView.ViewHolder {
public ViewHolder(LayoutInflater inflater, ViewGroup parent) {
super(inflater.inflate(R.layout.fragment_channel, parent, false));
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, ChannelDetailActivity.class);
context.startActivity(intent);
}
});
}
}
Just change getContext()
to v.getContext()
like this:
@Override
public void onClick(View v) {
v.getContext().startActivity(new Intent(v.getContext(), ChannelDetailActivity.class));
}
And make your ViewHolder class
an inner class of your ContentAdapter class
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With