I have a recycler view displaying all posts made by a user in my app. I want the users to be able to click on one of the posts and be brought to a new fragment where they can only see the post they have chosen.
When I run my code, and click on one of the posts, the OnCLickListener isn't even triggered.
I have tried adding break points on public void OnClick but it never gets triggered. I have tried adding the OnClick to the onBindViewHolder but I understand this isn't a good idea.
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.PostViewHolder> {
public List<Post> postList;
private Context context;
private static OnItemClickListener onItemClickListener;
public PostAdapter(){
}
public interface OnItemClickListener{
void onItemClick(View v, int position);
}
public void updateList(List<Post> list){
postList = list;
notifyDataSetChanged();
}
public static class PostViewHolder extends RecyclerView.ViewHolder{
TextView postName, postUsername, postDate, postTime, postContent, postId;
PostViewHolder(View v) {
super(v);
this.postName = (TextView) itemView.findViewById(R.id.name);
this.postUsername = (TextView) itemView.findViewById(R.id.username);
this.postDate = (TextView) itemView.findViewById(R.id.date);
this.postTime = (TextView) itemView.findViewById(R.id.time);
this.postContent = (TextView) itemView.findViewById(R.id.content);
this.postId = (TextView) itemView.findViewById(R.id.postId);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = PostViewHolder.super.getAdapterPosition();
onItemClickListener.onItemClick(v, position);
System.out.println("doesn't even make it here");
}
});
}
}
public PostAdapter(List<Post> postList, Context context){
this.postList = postList;
this.context = context;
}
#Override
public PostAdapter.PostViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = LayoutInflater.from(context).inflate(R.layout.all_posts_layout, parent, false);
return new PostAdapter.PostViewHolder(view);
}
#Override
public void onBindViewHolder(PostViewHolder holder, int position){
Post post = postList.get(position);
holder.postUsername.setText(String.valueOf(post.getUsername()));
holder.postName.setText(String.valueOf(post.getName()));
holder.postDate.setText(String.valueOf(post.getDate()));
holder.postTime.setText(String.valueOf(post.getTime()));
}
#Override
public int getItemCount(){
return this.postList.size();
}
Any help would be very much appreciated!
<---card layout for each individual post---->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="130dp"
android:orientation="vertical">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="130dp">
<android.support.v7.widget.CardView
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="2dp"
android:elevation="60dp">
<RelativeLayout
android:id="#+id/search_term_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dp">
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:text="Name"
android:textSize="20sp" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="italic"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/name"
android:text="Username"
android:textSize="18sp" />
<TextView
android:id="#+id/date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/content"
android:layout_alignParentEnd="true"
android:layout_marginEnd="56dp"
android:text="Date"
android:textSize="10sp" />
<TextView
android:id="#+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/username"
android:layout_toRightOf="#+id/username"
android:layout_alignParentEnd="true"
android:paddingLeft="8dp"
android:text="Time"
android:textSize="10sp" />
<TextView
android:id="#+id/content"
android:layout_width="315dp"
android:layout_height="48dp"
android:layout_below="#+id/name"
android:layout_alignStart="#+id/name"
android:text="Content"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/postId"
android:text="ID"
android:layout_below="#id/content"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</ScrollView>
</LinearLayout>
<---recycler view to display all posts------>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/filterBtn"
android:text="Filter"
android:background="#drawable/button"
android:layout_margin="2dp"
android:textColor="#color/common_google_signin_btn_text_dark_default"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/update_post_view"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:padding="10dp"
android:layout_height="match_parent" />
</RelativeLayout>
Seems like your ScrollView consumes click event. Try to remove it or replace with NestedScrollView
You need to set it in onCreateViewHolder on that view. Also init it in the constructor.
Update code according to this
public static class PostViewHolder extends RecyclerView.ViewHolder, View.OnClickListener {
TextView postName, postUsername, postDate, postTime, postContent, postId;
PostViewHolder(View v) {
super(v);
//init view
v.setOnClickListener(this);
}
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(v, getAdapterPosition());
}
}
**=> You just need to change onBindViewHolder() method code
=> Add click listener on this method.
=> Your code should be like this.**
#Override
public void onBindViewHolder(PostViewHolder holder, int position){
Post post = postList.get(position);
holder.postUsername.setText(String.valueOf(post.getUsername()));
holder.postName.setText(String.valueOf(post.getName()));
holder.postDate.setText(String.valueOf(post.getDate()));
holder.postTime.setText(String.valueOf(post.getTime()));
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = PostViewHolder.super.getAdapterPosition();
onItemClickListener.onItemClick(v, position);
System.out.println("doesn't even make it here");
}
});
}
Related
Here is my discussion.java which contains the RecyclerView
public class Discussion extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_discussion);
RecyclerView mRecycleview =(RecyclerView)findViewById(R.id.cycleview);
mRecycleview.setLayoutManager(new LinearLayoutManager(this));
String[] use ={"a","b"};
String[] lo={"be","ndls"};
String[] ti={"12:00","11:00"};
String[] staus={"Today","Tommmorow"};
int[] posimg={R.drawable.img_post1,R.drawable.img_post2};
mRecycleview.setAdapter(new FeedAdapter(use,lo,ti,staus,posimg));
#NonNull
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Open Activity for posting", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
// Functionality for poll
}
}
Here is my FeedAdapter for the RecyclerView which is taking from resource layout file content.xml.
public class FeedAdapter extends
RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {
private String[] userdata;
private String[] locdata;
private String[] timedata;
private int[] postdata;
private String[] statusdata;
public FeedAdapter(String[] userdata,String[] locdata,String[] timedata,String[] statusdata,int[] postdata){
this.userdata=userdata;
this.locdata=locdata;
this.postdata=postdata;
this.statusdata=statusdata;
this.timedata=timedata;
}
#Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view =inflater.inflate(R.layout.backupofcontent,parent,false);
return new FeedViewHolder(view);
}
#Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
String user=userdata[position];
String loc=locdata[position];
String time=timedata[position];
String status=statusdata[position];
int post=postdata[position];
holder.username.setText(user);
holder.locationa.setText(loc);
holder.timea.setText(time);
holder.statusa.setText(status);
if(post==0)
holder.postimg.setVisibility(View.GONE);
else
holder.postimg.setImageResource(post);
}
#Override
public int getItemCount() {
return userdata.length;
}
public class FeedViewHolder extends RecyclerView.ViewHolder{
ImageView postimg;
TextView username;
TextView locationa;
TextView timea;
TextView statusa;
public FeedViewHolder(View itemView) {
super(itemView);
postimg=itemView.findViewById(R.id.imgView_postPic);
username=itemView.findViewById(R.id.user_name);
locationa=itemView.findViewById(R.id.Location);
timea=itemView.findViewById(R.id.Time);
statusa=itemView.findViewById(R.id.post_text);
}
}
}
Here is my content.xml from which adapter is taking the feed. I want to set up the onClickListner on the view id:likelayout" or like so that when it is clicked, some actions are to be performed. For now, a toast would be enough.
Please help me out how to do it.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:orientation="vertical"
tools:showIn="#layout/app_bar_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="User"
android:textSize="20sp" />
<TextView
android:id="#+id/Time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/user_name"
android:layout_gravity="center|center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="Time"
android:textSize="10sp" />
<TextView
android:id="#+id/Location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/Time"
android:layout_gravity="center|center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="4dp"
android:text="Location"
android:textSize="10sp" />
<LinearLayout
android:id="#+id/layout_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Location"
android:layout_marginTop="10dp"
android:orientation="vertical">
<ImageView
android:id="#+id/imgView_postPic"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitCenter"
android:src="#drawable/img_post1" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="55dp"
android:layout_weight="1">
<TextView
android:id="#+id/post_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Status " />
</ScrollView>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginBottom="10dp"
android:gravity="center_vertical"
android:layout_above="#id/bottomnav">
<TextView
android:id="#+id/likecount"
android:layout_marginLeft="20dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="0"
android:gravity="center" />
<TextView
android:id="#+id/likecounttext"
android:layout_toRightOf="#id/likecount"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Liked"/>
<TextView
android:layout_toRightOf="#id/likecounttext"
android:layout_marginLeft="15dp"
android:id="#+id/countsepartor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"/>
<TextView
android:id="#+id/commentcount"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#id/countsepartor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"/>
<TextView
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/commentcount"
android:text="Comments"/>
</RelativeLayout>
<LinearLayout
android:id="#+id/bottomnav"
android:layout_width="match_parent"
android:layout_height="30dp"
android:weightSum="3"
android:layout_marginBottom="5dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/likelayout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="#drawable/like"/>
<TextView
android:id="#+id/like"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Like"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/commentlayout"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="#drawable/comment"/>
<TextView
android:id="#+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comment"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/polllayout"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center"
android:layout_gravity="center_vertical"
android:layout_height="wrap_content">
<ImageView
android:layout_width="25dp"
android:layout_height="wrap_content"
android:src="#drawable/poll"/>
<TextView
android:id="#+id/poll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Poll"
android:layout_marginLeft="10dp"
android:layout_gravity="center_vertical"
android:textSize="15sp"/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
In your onBindViewHolder function, just add a click listener like the following.
#Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
String user=userdata[position];
String loc=locdata[position];
String time=timedata[position];
String status=statusdata[position];
int post=postdata[position];
holder.username.setText(user);
holder.locationa.setText(loc);
holder.timea.setText(time);
holder.statusa.setText(status);
if(post==0)
holder.postimg.setVisibility(View.GONE);
else
holder.postimg.setImageResource(post);
holder.username.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Do something.
}
});
}
If you want to set up the click listener to the other UI elements in your list item, then just declare them like the way you have declared username, timea, statusa etc. and then add the listener to it.
Please keep in mind that, in order to make an ImageView clickable you need to add the clickable attribute to true in your layout file. For example, if you need to make your like image clickable, you need to add the clickable attribute like the following.
<ImageView
android:id="#+id/like_image"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:clickable="true"
android:src="#drawable/like"/>
In order to show a Toast on clicking the item, you need to have the Context of the Discussion activity while you are creating the adapter. So I would like to suggest you modify the constructor of the adapter like the following.
public class FeedAdapter extends
RecyclerView.Adapter<FeedAdapter.FeedViewHolder> {
private String[] userdata;
private String[] locdata;
private String[] timedata;
private int[] postdata;
private String[] statusdata;
// Add another extra variable to store context
// The context is necessary for showing toast
private Context context;
public FeedAdapter(String[] userdata, String[] locdata,String[] timedata, String[] statusdata, int[] postdata, Context context){
this.userdata = userdata;
this.locdata = locdata;
this.postdata = postdata;
this.statusdata = statusdata;
this.timedata = timedata;
this.context = context; // Initialize it here
}
#Override
public FeedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater inflater=LayoutInflater.from(parent.getContext());
View view =inflater.inflate(R.layout.backupofcontent,parent,false);
return new FeedViewHolder(view);
}
#Override
public void onBindViewHolder( FeedAdapter.FeedViewHolder holder, int position) {
String user=userdata[position];
String loc=locdata[position];
String time=timedata[position];
String status=statusdata[position];
int post=postdata[position];
holder.username.setText(user);
holder.locationa.setText(loc);
holder.timea.setText(time);
holder.statusa.setText(status);
if(post==0)
holder.postimg.setVisibility(View.GONE);
else
holder.postimg.setImageResource(post);
// Use the context here to show the Toast
holder.username.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(context, "Something", Toast.LENGTH_LOGN).show();
}
});
}
#Override
public int getItemCount() {
return userdata.length;
}
public class FeedViewHolder extends RecyclerView.ViewHolder{
ImageView postimg;
TextView username;
TextView locationa;
TextView timea;
TextView statusa;
public FeedViewHolder(View itemView) {
super(itemView);
postimg=itemView.findViewById(R.id.imgView_postPic);
username=itemView.findViewById(R.id.user_name);
locationa=itemView.findViewById(R.id.Location);
timea=itemView.findViewById(R.id.Time);
statusa=itemView.findViewById(R.id.post_text);
}
}
}
And to adopt the changes of the modified constructor, the adapter needs to be initialized like the following from your Discussion class.
mRecycleview.setAdapter(new FeedAdapter(use, lo, ti, staus, posimg, this));
Hope that helps!
same as the other views, in the FeedViewHolder class add a LinearLayout variable:
LinearLayout like;
then
like = itemView.findViewById(R.id.likelayout);
then in onBindViewHolder use :
holder.like.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Like Layout was clicked
}
});
So I am displaying a recylerview with an Image and the name of a person. Below that is a seekbar, which the user can move to rate that person.
Now I want to store the ratings (=progress) of each seekbar in a list. However right now it only stores the rating of the last seekbar in the list.
So it stores only the progress of the last seekbar right now. I would like that when the user clicks the button that every current rating value of every seekbar is stored in a list.
Thank you very much.
That is is my layout_listitem:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="3dp"
android:id="#+id/parent_layoutREC"
android:orientation="horizontal"
android:layout_marginTop="13dp"
android:gravity="center">
<de.hdodenhof.circleimageview.CircleImageView
android:paddingTop="2dp"
android:paddingLeft="2dp"
android:layout_width="73dp"
android:layout_height="73dp"
android:id="#+id/kunde_imageREC"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_marginLeft="40dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Canada"
android:id="#+id/kunde_nameREC"
android:textColor="#000"
android:textSize="19sp"
android:textStyle="bold"/>
</LinearLayout>
<SeekBar
android:layout_marginRight="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="8dp"
android:id="#+id/seek_Bar"
android:max="10"
android:progress="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="0 1 2 3 4 5"
android:textSize="20sp"/>
That is my RecyclerViewAdapter class:
public class RecyclerViewAdap extends
RecyclerView.Adapter<RecyclerViewAdap.ViewHolder> {
private static final String TAG = "RecyclerViewAdap";
private ArrayList<String> mImageUrl = new ArrayList<>();
private ArrayList<String> mKundeNamen = new ArrayList<>();
public ArrayList<Integer> mBewertungen = new ArrayList<>();
private Context mContext;
public String bewertung;
private TextView progressSeekbar;
public RecyclerViewAdap(ArrayList<String> imageUrl, ArrayList<String> kundeNamen, Context context) {
mImageUrl = imageUrl;
mKundeNamen = kundeNamen;
mContext = context;
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView imageView;
TextView kundeName;
LinearLayout parentLayout;
SeekBar seekBar1;
public ViewHolder(#NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.kunde_imageREC);
kundeName = itemView.findViewById(R.id.kunde_nameREC);
parentLayout = itemView.findViewById(R.id.parent_layoutREC);
seekBar1 = itemView.findViewById(R.id.seek_Bar);
progressSeekbar = itemView.findViewById(R.id.progress_seekbar);
seekBar1.setOnSeekBarChangeListener(seekBarChangeListener);
//int progress = seekBar1.getProgress();
//String.valueOf(Math.abs((long)progress)).charAt(0);
//progressSeekbar.setText("Bewertung: " +
// String.valueOf(Math.abs((long)progress)).charAt(0) + "." + String.valueOf(Math.abs((long)progress)).charAt(1));
}
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_listitem, parent, false);
ViewHolder holder = new ViewHolder(view);
Log.d(TAG, "onCreateViewHolder: ");
return holder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, final int position) {
Glide.with(mContext)
.load(mImageUrl.get(position))
.into(holder.imageView);
holder.kundeName.setText(mKundeNamen.get(position));
holder.parentLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(mContext, mKundeNamen.get(position),
Toast.LENGTH_SHORT).show();
}
});
holder.seekBar1.setTag(position);
}
#Override
public int getItemCount() {
return mImageUrl.size();
}
SeekBar.OnSeekBarChangeListener seekBarChangeListener = new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
// updated continuously as the user slides the thumb
progressSeekbar.setText(String.valueOf(progress));
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// called when the user first touches the SeekBar
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// called after the user finishes moving the SeekBar
if (seekBar.getTag().toString().equals("1")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 2) {
mBewertungen.remove(0);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
if (seekBar.getTag().toString().equals("2")) {
mBewertungen.add(seekBar.getProgress());
if (mBewertungen.size() == 3) {
mBewertungen.remove(1);
Toast.makeText(mContext, "onProgressChanged: " + mBewertungen.toString(), Toast.LENGTH_SHORT).show();
}
}
}
};
}
That is the activity where the Recyclerview is being displayed:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".BewertungEsserActvity"
android:orientation="vertical"
app:layoutManager="LinearLayoutManager"
android:background="#drawable/gradient_background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar"
layout="#layout/toolbar" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Bewerte deine Gäste"
android:textColor="#color/colorDarkGrey"
android:textSize="30sp"
android:textStyle="bold" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorBlack"
android:layout_marginBottom="10dp"/>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/recycler_view">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:visibility="invisible"
android:id="#+id/progressbar_4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="13dp" />
<Button
android:layout_marginRight="66dp"
android:layout_marginLeft="66dp"
android:id="#+id/bewerten_Btn"
android:layout_width="match_parent"
android:layout_height="55dp"
android:layout_marginTop="10dp"
android:background="#drawable/btn_background_profil"
android:padding="10dp"
android:text="Bewerten"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:textSize="16sp"
android:textStyle="normal" />
<View
android:layout_marginBottom="10dp"
android:layout_marginTop="34dp"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/colorBlack" />
</LinearLayout>
</ScrollView>
You can do it in multiple ways:
1- You can store seekbars value as a property in your model and update that property on OnSeekBarChangeListener and when the user hits the button get the data source from the adapter and then run a loop on it and call getter of that attribute.
2- You can store seekbars value as a List<> in your adapter and whenever the user hits the button get the list from the adapter.
I have a cardview, with some widgets in, and a recycler view inside the card view. The cardview is the parent.
I want it to be that when anywhere in the cardview is clicked, the event to happen. However, now only when the top part of the card view is clicked it triggers, but not when I click on the recycler view.
I tried setting clickable in the recyclerview to false, and it doesn't work.
Here is my xml.
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view_today"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="wrap_content"
card_view:cardCornerRadius="4dp"
android:padding="5dp"
android:clickable="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:clickable="false"
android:gravity="center_vertical"
>
<ImageView
android:layout_width="20dp"
android:layout_marginLeft="5dp"
android:layout_height="20dp"
android:clickable="false"
android:src="#drawable/today_icon_small"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:text="I still need to have today"
android:layout_margin="5dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/today_recycler_view_summary"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:clickable="false"
android:paddingBottom="10dp">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
</android.support.v7.widget.CardView>
And here is the code of the click listener I do on the activity oncreate():
todayCardView = (CardView) findViewById(R.id.card_view_today);
todayCardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "Today card tapped");
Intent intent = new Intent(MainActivity.this, TodayActivity.class);
startActivity(intent);
}
});
As requsted, here is my adapter and view holder, for the recycler view, which is in the card view.
public class SummaryRecyclerViewAdapter extends RecyclerView.Adapter<SummaryRecyclerViewAdapter.ViewHolder> {
String[] todaysItems;
public SummaryRecyclerViewAdapter(String[] todaysItems) {
this.todaysItems = todaysItems;
}
#Override
public SummaryRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_view_chip,parent,false);
return new SummaryRecyclerViewAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(SummaryRecyclerViewAdapter.ViewHolder holder, int position) {
holder.textView.setText(todaysItems[position]);
}
#Override
public int getItemCount() {
return todaysItems.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public TextView textView;
public ViewHolder(View v){
super(v);
textView = (TextView)v.findViewById(R.id.chip_text_view);
}
}
}
Add this for the recycler view .This makes sure that the child view will not handle the touch event .
#Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
return false;
}
Read this article to better understand the touch input flow .
http://balpha.de/2013/07/android-development-what-i-wish-i-had-known-earlier/
I have an adapter with buttons and text views. It represents friend requests.
TextView text would be : Do you want to accept X as your friend?
There are 2 buttons : one for accepting, one for declining. Pressing Decline, it would just remove the row from the ListView, while pressing accept will change the text into: X and Y are now friends.
My question is how can I get the position of the row and change it on button press and also save the View for next time I access it?
Adapter : At the moment I don't have proper data structure for the adapter, so for testing purposes, just using a String:
public class FriendRequestAdapter extends BaseAdapter {
String mName;
Context mContext;
public FriendRequestAdapter(String name, Context context) {
this.mName = name;
this.mContext = context;
}
#Override
public int getCount() {
return 1;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater)
mContext.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.friend_request_layout, null);
}
TextView sender_name = (TextView) convertView.findViewById(R.id.wannabe_name);
if (mName.length() > 11) {
mName = mName.substring(0, 12);
}
sender_name.setText(mName);
ImageButton friend_request_accepted = (ImageButton) convertView.findViewById(R.id.accept_friend_request);
friend_request_accepted.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new AsyncTask<Void, Void, Void>() {
#Override
protected Void doInBackground(Void... params) {
try {
new APIService().confirmInvite();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
};
}
});
return convertView;
}
Inflated Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/changing_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/check_in_map_menu"
android:descendantFocusability="blocksDescendants">
<RelativeLayout
android:id="#+id/friend_image_container"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginLeft="20dp"
android:layout_marginTop="7dp"
android:background="#drawable/polaroid_frame_friend_list">
<ImageView
android:id="#+id/facebook_friend_pic"
android:layout_width="55dp"
android:layout_height="45dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/category_explore"/>
<TextView
android:id="#+id/name_facebook"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/facebook_friend_pic"
android:layout_centerHorizontal="true"
android:text="dummy"
android:textColor="#color/enloop_dark_gray"
android:textSize="9sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="7dp"
android:layout_toRightOf="#+id/friend_image_container">
<TextView
android:id="#+id/wannabe_name"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:text="dada"
android:textColor="#color/enloop_dark_gray"
android:textSize="15dp"/>
<TextView
android:id="#+id/facebook_friend_name"
android:layout_width="250dp"
android:layout_height="50dp"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/wannabe_name"
android:text=" has sent you a request"
android:textColor="#color/enloop_dark_gray"
android:textSize="15dp"/>
<ImageButton
android:id="#+id/later_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:background="#drawable/btn_nothanks"
android:clickable="false"
android:focusable="false"/>
<ImageButton
android:id="#+id/accept_friend_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="40dp"
android:layout_toRightOf="#+id/later_button"
android:background="#drawable/btn_accept_text"
android:clickable="false"
android:focusable="false"/>
<ImageButton
android:id="#+id/invite_facebook_friends"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:background="#drawable/btn_delete_notification"
android:clickable="false"
android:focusable="false"/>
</RelativeLayout>
</RelativeLayout>
I am trying to add a custom header that isnt clickable but will have a checkbox that will "check all" checkboxes under it.
This is my List Fragment
public class AssesmentListFragment extends ListFragment {
private static String BUNDLE_KEY_APPLICATION = "LIST_ITEM";
FastAssesmentListAdapter adapter;
View listHeader;
public AssesmentListFragment() {}
public AssesmentListFragment(Data[] data) {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Data[] assestments = {new Data("Assesment ID","Name", "Date"), new Data("123456", "Assestment 2", "9/12/12"),
new Data("345672", "Assesment 3", "9/13/12"), new Data("566893", "Assesment 4", "9/14/12")};
//This is the part that makes the app crash
View header = getActivity().getLayoutInflater().inflate(R.layout.list_adapter_assesments, null);
ListView listView = getListView();
listView.addHeaderView(header);
adapter = new FastAssesmentListAdapter(getActivity(), assestments);
setListAdapter(adapter);
updateList(assestments);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
private void updateList(Data[] assestments) {
// NOTE: addAll is not being used to support pre-honeycomb devices
synchronized(adapter) {
adapter.clear();
adapter.addAll(assestments);
adapter.notifyDataSetChanged();
}
}
#Override
public void onListItemClick(ListView parentView, View selectedItemView, int position, long id) {
String model = (String) parentView.getItemAtPosition(position);
((FacilityActivity) getActivity()).onItemSelected(model);
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
//outState.putInt("curChoice", mCurCheckPosition);
}
}
This is the layout I am trying to use for header
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:paddingRight="10dp"
android:orientation="horizontal">
<TextView android:id="#+id/adapter_header_textview_column1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textColor="#color/defaultTextColor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="28sp"
android:text="Assesment ID" />
<TextView android:id="#+id/adapter_header_textview_column2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textColor="#color/defaultTextColor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="28sp"
android:text="Name" />
<TextView android:id="#+id/adapter_header_textview_column3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textColor="#color/defaultTextColor"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="28sp"
android:text="Date"/>
<CheckBox
android:id="#+id/header_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:color="#color/defaultTextColor"
android:layout_weight=".5"
android:gravity="center" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="5dp"
android:background="#color/BPGreenColor" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Then this is the array adapter I am using:
public class FastAssesmentListAdapter extends ArrayAdapter<Data> {
private static int LAYOUT_ID = R.layout.list_adapter_with_checkbox_three_column;
private final Data[] assesments;
private final Context context;
LinearLayout listHeader;
static class ViewHolder {
protected TextView column1;
protected TextView column2;
protected TextView column3;
protected CheckBox checkbox;
}
public FastAssesmentListAdapter(Context context, Data[] assesments) {
super(context, LAYOUT_ID, assesments);
this.context = context;
this.assesments = assesments;
}
//ListFragment and array adapter will automatically call this over and over to auto populate the list
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final Data item = getItem(position);
// Formulate row view (create if it does not exist yet)
View view = convertView;
if(view == null) {
LayoutInflater inflater = ((Activity) getContext()).getLayoutInflater();
view = inflater.inflate(LAYOUT_ID, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.column1 = (TextView) view.findViewById(R.id.adapter_textview_column1);
viewHolder.column2 = (TextView) view.findViewById(R.id.adapter_textview_column2);
viewHolder.column3 = (TextView) view.findViewById(R.id.adapter_textview_column3);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check_box);
view.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Clicked",
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(getContext(), FacilityActivity.class);
getContext().startActivity(intent);
}
});
if(viewHolder.checkbox != null) {
viewHolder.checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked) {
item.setSelected(isChecked);
Toast.makeText(getContext(), "Checked",
Toast.LENGTH_SHORT).show();
}
}
});
}
view.setTag(viewHolder);
viewHolder.checkbox.setTag(position);
}
ViewHolder viewHolder = (ViewHolder) view.getTag();
viewHolder.checkbox.setTag(position);
viewHolder.column1.setText(item.getColumn1());
viewHolder.column2.setText(item.getColumn2());
viewHolder.column3.setText(item.getColumn3());
viewHolder.checkbox.setChecked(item.isSelected());
return view;
}
}
on a side note, the onlistitemclicked in the fragment doesnt work, i have to set a listener in the adapter and then it works. any ideas on that? but mainly I need to figure out how to use a custom header and custom rows in the list view. Here is the layout for the rows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="25dp"
android:paddingRight="10dp"
android:orientation="horizontal">
<TextView android:id="#+id/adapter_textview_column1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<TextView android:id="#+id/adapter_textview_column2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<TextView android:id="#+id/adapter_textview_column3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25sp" />
<CheckBox
android:id="#+id/check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:gravity="center" />
</LinearLayout>