如果单击该复选框,则表示在滚动期间将取消选中该选项,反之亦然。此外,我想在单击复选框后刷新主页片段。有人提供解决方案来解决这两个问题吗?
customViewHolder.checkBox.setChecked(feedItem.isSelected());
customViewHolder.checkBox.setTag(position);
customViewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//set your object's last status
//feedItem.setSelected(isChecked);
//customViewHolder.checkBox.setTag(position);
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
feedItemList.get(getPosition).setSelected(buttonView.isChecked()); // Set the value of checkbox to maintain its state.
}
});
if(!feedItem.getLikedStatus().isEmpty()) {
if(feedItem.getLikedStatus().matches("liked")) {
customViewHolder.checkBox.setChecked(true);
} else {
customViewHolder.checkBox.setChecked(false);
}
}
//set Like Count
if(!feedItem.getLikedCount().isEmpty()) {
if(feedItem.getLikedCount().matches("1")) {
customViewHolder.likedUserName.setVisibility(View.VISIBLE);
customViewHolder.likedUserName.setText(feedItem.getLikedUserName());
customViewHolder.likeImageView.setVisibility(View.VISIBLE);
} else if(feedItem.getLikedCount().matches("0")) {
customViewHolder.likedUserName.setVisibility(View.GONE);
customViewHolder.likeImageView.setVisibility(View.GONE);
} else {
customViewHolder.likedUserName.setVisibility(View.VISIBLE);
customViewHolder.likedUserName.setText(feedItem.getLikedCount()+" likes");
customViewHolder.likeImageView.setVisibility(View.VISIBLE);
}
}
customViewHolder.checkBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!feedItem.getLikedCount().isEmpty()) {
if(feedItem.getLikedCount().matches("0")) {
if(customViewHolder.checkBox.isChecked()) {
customViewHolder.likeImageView.setVisibility(View.VISIBLE);
customViewHolder.likedUserName.setVisibility(View.VISIBLE);
customViewHolder.likedUserName.setText(userName);
//call like url
postLikeUnlike(feedItem.getPostID(),like);
} else {
//call unlike url
customViewHolder.likeImageView.setVisibility(View.GONE);
customViewHolder.likedUserName.setVisibility(View.GONE);
postLikeUnlike(feedItem.getPostID(), unlike);
}
} else {
if(customViewHolder.likedUserName.getText().toString().matches(userName)) {
if (customViewHolder.checkBox.isChecked()) {
customViewHolder.likeImageView.setVisibility(View.VISIBLE);
customViewHolder.likedUserName.setVisibility(View.VISIBLE);
customViewHolder.likedUserName.setText(userName);
//call like url
postLikeUnlike(feedItem.getPostID(),like);
} else {
customViewHolder.likeImageView.setVisibility(View.GONE);
customViewHolder.likedUserName.setVisibility(View.GONE);
//call unlike url
postLikeUnlike(feedItem.getPostID(),unlike);
}
} else {
if (customViewHolder.checkBox.isChecked()) {
System.out.println("checkbox already checked");
customViewHolder.i = customViewHolder.i + 1;
customViewHolder.likedUserName.setText(String.valueOf(customViewHolder.i) + " likes");
//call like url
postLikeUnlike(feedItem.getPostID(),like);
} else {
System.out.println("i1 value" + customViewHolder.i);
customViewHolder.i = customViewHolder.i - 1;
customViewHolder.likedUserName.setText(String.valueOf(customViewHolder.i) + " likes");
//call unlike url
postLikeUnlike(feedItem.getPostID(),like);
}
}
}
}
}
});
答案 0 :(得分:0)
我为RatingBar面临同样的问题。 这是我的代码。 您只需设置CHECKBOX而不是RatingBar。
public class MainActivity extends AppCompatActivity {
String ratedValue;
ListView ratingList;
private static final String[] listItems = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11","12", "13", "14","15", "16", "17", "18", "19", "20" };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_activity);
ratingList = (ListView)findViewById(R.id.ratingList);
ArrayList<RowModel> list = new ArrayList<RowModel>();
for (String s : listItems) {
list.add(new RowModel(s));
}
ratingList.setAdapter(new RatingAdapter(list));
}
private RowModel getModel (int position) {
return ((RatingAdapter)ratingList.getAdapter()).getItem(position);
}
class RatingAdapter extends ArrayAdapter<RowModel> {
RatingAdapter(ArrayList<RowModel> list) {
super(MainActivity.this, R.layout.list_row, R.id.idView, list);
}
class ViewHolder {
RatingBar ratingBar = null;
ViewHolder(View view) {
this.ratingBar = (RatingBar)view.findViewById(R.id.ratingBar1);
this.ratingBar.setEnabled(false);
}
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = super.getView(position, convertView, parent);
ViewHolder holder = (ViewHolder)row.getTag();
if (holder == null) {
holder = new ViewHolder(row);
row.setTag(holder);
RatingBar.OnRatingBarChangeListener l = new RatingBar.OnRatingBarChangeListener() {
@Override
public void onRatingChanged(RatingBar ratingBar, float rating, boolean fromUser) {
Integer myPosition = (Integer)ratingBar.getTag();
RowModel model = getModel(myPosition);
model.rating = rating;
LinearLayout parent = (LinearLayout)ratingBar.getParent();
TextView label = (TextView)parent.findViewById(R.id.ratingMessage);
label.setText("Rating : " + rating + "/5");
}
};
holder.ratingBar.setOnRatingBarChangeListener(l);
}
RowModel model = getModel(position);
holder.ratingBar.setTag(new Integer(position));
holder.ratingBar.setRating(model.rating);
return (row);
}
}
class RowModel {
String label;
float rating = 0.0f;
RowModel(String label) {
this.label = label;
}
public String toString() {
if (rating >= 3.0) {
return (label.toUpperCase());
}
return label;
}
}
}