当前位置: 首页 > 知识库问答 >
问题:

回收者查看项目被更改时我滚动回收者视图

龙高超
2023-03-14

当我在回收器视图中选择一个项目时,会自动选择多个项目

我正在做的是

我只想获取没有重复的联系人,并在recyclerview中列出所有联系人,并使两个文本视图可见(一个用于数字,第二个用于名称)。完成了,但问题是

当我点击一个项目时,它会被点击4次(例如,如果我点击一个项目,它会显示两个文本视图,但具有相同名称和编号的相同项目会显示出来,就像我点击了4个项目并看到图片一样)

适配器类

    package "";

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.altu.calllocker.R;
import com.example.altu.calllocker.model.ContactsModel;

import java.util.HashMap;
import java.util.List;


public class ContactsAdapter extends RecyclerView.Adapter<ContactsAdapter.ContactsViewHolder> {
    List<ContactsModel> list;
    Context context;
    public static HashMap<String, String> hashMapBlock;

    public ContactsAdapter(List<ContactsModel> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public ContactsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.contacts_item, parent, false);
        context = parent.getContext();
        hashMapBlock = new HashMap<>();
        ContactsViewHolder viewHolder = new ContactsViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final ContactsViewHolder holder, final int position) {
        holder.txtno.setText(list.get(position).get_id());
        holder.txt_contactname.setText(list.get(position).getDisplay_name());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.e("position", list.get(position).get_id() + "  " + list.get(position).getData1() + "  " + list.get(position).getDisplay_name());
                if (holder.linearLayout.getVisibility() == View.VISIBLE) {
                    holder.linearLayout.setVisibility(View.GONE);
                } else {
                    holder.linearLayout.setVisibility(View.VISIBLE);
                    holder.txt_contactName.setText(list.get(position).getDisplay_name());
                    holder.txt_contact_no.setText(list.get(position).getData1());
                }

            }
        });
        holder.switchBlock.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    hashMapBlock.put(list.get(position).getData1(), "block");
                    Log.e("block", list.get(position).getData1());
                    Toast.makeText(context, "block" + "  " + list.get(position).getData1(), Toast.LENGTH_SHORT).show();
                } else {
                    hashMapBlock.remove(list.get(position).getData1());
                    Log.e("unblock", list.get(position).getData1());
                    Toast.makeText(context, "unblock" + "  " + list.get(position).getData1(), Toast.LENGTH_SHORT).show();
                }
            }
        });

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ContactsViewHolder extends RecyclerView.ViewHolder {
        TextView txt_contactname;
        TextView txt_contactName;
        TextView txt_contact_no;
        LinearLayout linearLayout;
        SwitchCompat switchBlock;
        TextView txtno;

        public ContactsViewHolder(View itemView) {

            super(itemView);
            this.txtno = (TextView) itemView.findViewById(R.id.txtno);
            this.txt_contactname = (TextView) itemView.findViewById(R.id.txt_contactName);
            this.txt_contactName = (TextView) itemView.findViewById(R.id.txt_contact_name);
            this.txt_contact_no = (TextView) itemView.findViewById(R.id.txt_contact_no);
            this.linearLayout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
            this.switchBlock = (SwitchCompat) itemView.findViewById(R.id.switchButton_block);


        }
    }
}

在那里我得到所有联系人到回收人员视图

 package "";

import android.app.ProgressDialog;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.widget.Toast;

import com.example.altu.calllocker.R;
import com.example.altu.calllocker.adapter.ContactsAdapter;
import com.example.altu.calllocker.customclass.SimpleDividerItemDecoration;
import com.example.altu.calllocker.model.ContactsModel;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerview_contacts;
    List<ContactsModel> contactVOList;
    ProgressDialog progressDialog;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        bindview();
        new Thread(new Task()).start();
        progressDialog.dismiss();


        //adapter = new ContactsAdapter(contactlist, getApplicationContext());
        //recyclerview_contacts.setAdapter(adapter);

    }

    public void bindview() {
        recyclerview_contacts = (RecyclerView) findViewById(R.id.recyclerview_contacts);
        contactVOList = new ArrayList<>();
        recyclerview_contacts.setHasFixedSize(true);
        LinearLayoutManager llm = new LinearLayoutManager(MainActivity.this);
        llm.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerview_contacts.setLayoutManager(llm);
        SimpleDividerItemDecoration itemDecoration = new SimpleDividerItemDecoration(this);
        recyclerview_contacts.addItemDecoration(itemDecoration);
        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Loading.....");
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMax(100);
        progressDialog.setProgress(0);
        progressDialog.show();
    }

    class Task implements Runnable {
        @Override
        public void run() {

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    getAllContact();
                }
            });


        }

    }



    private void getAllContact() {
        if (contactVOList.size() > 0) {
            contactVOList.clear();
        }
        //  List<ContactsModel> contactVOList = new ArrayList();
        ContactsModel contactVO;

        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC");
        if (cursor.getCount() > 0) {
            while (cursor.moveToNext()) {

                int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER)));
                if (hasPhoneNumber > 0) {
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    contactVO = new ContactsModel();
                    contactVO.setDisplay_name(name);
                    contactVO.set_id(id);

                    Cursor phoneCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?",
                            new String[]{id},
                            null);
                    if (phoneCursor.moveToNext()) {
                        String phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                        contactVO.setData1(phoneNumber);
                    }

                    phoneCursor.close();

                    Cursor emailCursor = contentResolver.query(
                            ContactsContract.CommonDataKinds.Email.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?",
                            new String[]{id}, null);
                    while (emailCursor.moveToNext()) {
                        String emailId = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                    }
                    emailCursor.close();
                    contactVOList.add(contactVO);
                }

            }

            ContactsAdapter contactAdapter = new ContactsAdapter(contactVOList, getApplicationContext());
            //recyclerview_contacts.setLayoutManager(new LinearLayoutManager(this));
            recyclerview_contacts.setAdapter(contactAdapter);
            contactAdapter.notifyDataSetChanged();
            Log.e("size", String.valueOf(contactVOList.size()));
        }
    }

}

带自动选择的图像1图像2

共有1个答案

萧鹏云
2023-03-14

您应该在ViewHolder中设置OnclickListener并使用getLayoutPotion()方法获取被点击的Item的位置。

 public class ContactsViewHolder extends RecyclerView.ViewHolder {
    TextView txt_contactname;
    TextView txt_contactName;
    TextView txt_contact_no;
    LinearLayout linearLayout;
    SwitchCompat switchBlock;
    TextView txtno;

    public ContactsViewHolder(View itemView) {

        super(itemView);
        this.txtno = (TextView) itemView.findViewById(R.id.txtno);
        this.txt_contactname = (TextView) itemView.findViewById(R.id.txt_contactName);
        this.txt_contactName = (TextView) itemView.findViewById(R.id.txt_contact_name);
        this.txt_contact_no = (TextView) itemView.findViewById(R.id.txt_contact_no);
        this.linearLayout = (LinearLayout) itemView.findViewById(R.id.linearlayout);
        this.switchBlock = (SwitchCompat) itemView.findViewById(R.id.switchButton_block);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 int position =getLayoutPosition();
                //Your logic

            }
        });


    }
}
 类似资料:
  • 所以,这就是我的问题。我的recyclerview项目在底部有一个视图,我最初将其设置为消失。现在,当它们被点击时,我想让它们再次可见。所以在onClick方法中,我将视图设置为Visible。一切正常,但是当我向下滚动和向上滚动时,视图又隐藏了。我想这和取景模式有关。我想保持现状,即开放。我该怎么做?谢了。 视图支架: 点击: 编辑:上传的代码。此外,我尝试在onClick中更新Item的布尔值

  • 我正在从事一个自由职业者的android项目。我有一个问题,就是根据项目设计需要改变RecyclerView项目之间的利润率。我想显示屏幕上最后一个可见项目的一半,这样用户就可以猜测水平滚动。我尝试创建一个自定义的RecyclerView.itemEdition。当屏幕第一次打开时,一切正常,但当我开始在这个回收视图上水平滚动时,项目之间的间隔增加了,有时甚至减少了。我如何用一般方法解决这个问题。

  • 在这里我有项目的列表在回收器视图这是取自Firebase.所以我试图改变其背景颜色当用户点击它.但是当我点击项目1然后项目4背景颜色也得到改变.如果点击项目2然后项目8颜色也.它给不恰当的结果。 视图持有者类

  • 如果用户点击一个,我希望它做一些事情,也为两个和三个。我如何实现这一点?

  • 我正在使用使用从中提供数据的。我想禁用事件的动画,但将它们保留为//。我尝试在使用的上调用,但动画仍然出现。如果我调用,所有动画都会按预期成功删除。 我尝试查看实现,似乎如果是,将通过保留旧的viewHolder并将其交叉淡出到新的viewHolder来对更改事件进行动画处理。我不想这样。如果是,则旧的和新的viewHolders将是相同的对象,取而代之的是从x到x的动画(即没有实际移动)。然而,

  • 我想展开/折叠我的recyclerView项目,以显示更多信息。我想实现与SlideExpandableListView相同的效果。 基本上,在我的viewHolder中,我有一个不可见的视图,我想做一个平滑的展开/折叠动画,而不是将可见性设置为仅可见/消失。我一次只需要扩展一个项目,如果有一些提升来显示该项目已被选中,那就太酷了。 这与新的Android最近通话历史记录列表的效果相同。“回调”和