可能是愚蠢的问题,但我找不到为什么我的RecyclerView不响应点击的原因。 RV在一个片段中,我在适配器中添加了一个接口,并在片段中实现了它。但是,在尝试单击时,模拟器甚至都没有发出“点击”声音
这是我的适配器
from selenium import webdriver as wd
import pytest
import time
Chrome=wd.Chrome(executable_path=r"C:\Chrome\chromedriver.exe")
Firefox=wd.Firefox(executable_path=r"C:\geckodriver\geckodriver.exe")
class TestLogin():
@pytest.fixture()
def setup1(self):
browsers=[Chrome, Firefox]
for i in browsers:
self.driver= i
i.get("https://www.python.org")
time.sleep(3)
yield
time.sleep(3)
self.driver.close()
def test_Python_website(self,setup1):
self.driver.find_element_by_id("downloads").click()
time.sleep(3)
}
这是片段
public class ArtistsRvAdapter extends RecyclerView.Adapter<ArtistsRvAdapter.ArtistViewHolder> {
private List<Artist> mArtists;
private Context mContext;
final private ItemClickListener mItemClickListener;
public ArtistsRvAdapter(Context c, ItemClickListener listener){
mContext=c;
mItemClickListener =listener;
}
@NonNull
@Override
public ArtistsRvAdapter.ArtistViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
int layoutIdForListItem = R.layout.artist_list_item;
LayoutInflater inflater = LayoutInflater.from(mContext);
boolean shouldAttachToParentImmediately = false;
View view = inflater.inflate(layoutIdForListItem, viewGroup, shouldAttachToParentImmediately);
ArtistViewHolder viewHolder = new ArtistViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ArtistViewHolder artistViewHolder, int position) {
Artist artist = this.mArtists.get(position);
Log.d("rv adapter ", "artist img " + artist.getArtistImageUrl());
String image = artist.getArtistImageUrl();
Picasso.get()
.load(image)
.placeholder(R.drawable.ic_launcher_background)
.error(R.drawable.ic_launcher_foreground)
.into(artistViewHolder.artistImage);
artistViewHolder.artistName.setText(artist.getArtistName());
Log.d("rv adapter ", "artist name " + artist.getArtistName());
artistViewHolder.artistGenre.setText(artist.getArtistGenre());
artistViewHolder.concertDate.setText(artist.getConcertDate());
artistViewHolder.concertLocation.setText(artist.getConcertLocation());
}
@Override
public int getItemCount() {
return (null != mArtists ? mArtists.size() : 0);
}
public class ArtistViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView artistImage;
TextView artistName;
TextView artistGenre;
TextView concertLocation;
TextView concertDate;
public ArtistViewHolder(View itemView){
super(itemView);
artistImage = itemView.findViewById(R.id.artist_image);
artistName = itemView.findViewById(R.id.artist_name);
artistGenre = itemView.findViewById(R.id.artist_genre);
concertLocation = itemView.findViewById(R.id.concert_location);
concertDate = itemView.findViewById(R.id.concert_date);
}
@Override
public void onClick(View v) {
String elementID = mArtists.get(getAdapterPosition()).getKey();
Log.d(" adapter " , "position clicked " + getAdapterPosition());
mItemClickListener.onItemClickListener(elementID);
}
}
public interface ItemClickListener {
void onItemClickListener(String itemId);
}
public void setArtists(List<Artist> artists){
mArtists = artists;
Log.d("rv " , "list artists " + mArtists);
notifyDataSetChanged();
}
}
这是xml
public class BaseFragment extends Fragment implements ArtistsRvAdapter.ItemClickListener {
private ArtistsRvAdapter mAdapter;
private String LOG_TAG = BaseFragment.class.getSimpleName();
private View mRootView;
@BindView(R.id.rv)
RecyclerView recyclerView;
private Context mContext;
private AppDatabase mDb;
public BaseFragment(){}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mRootView= inflater.inflate(R.layout.base_fragment, container, false);
ButterKnife.bind(this, mRootView);
mContext = getActivity().getApplicationContext();
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(mContext);
mDb = AppDatabase.getInstance(getActivity());
mAdapter = new ArtistsRvAdapter(mContext, this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(mAdapter);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(recyclerView.getContext(),
((LinearLayoutManager) layoutManager).getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
setupViewModel();
return mRootView;
}
private void setupViewModel(){
Log.d(LOG_TAG, "getting artists from DB");
DetailsViewModel viewModel = ViewModelProviders.of(getActivity()).get(DetailsViewModel.class);
viewModel.getArtists().observe(this, new Observer<List<Artist>>() {
@Override
public void onChanged(List<Artist> artists) {
mAdapter.setArtists(artists);
}
});
}
@Override
public void onItemClickListener(String itemId) {
Intent i = new Intent(getActivity(), DetailsActivity.class);
i.putExtra("artistId", itemId);
startActivity(i);
}
答案 0 :(得分:1)
在ArtistViewHolder
之后在您的super(itemView);
中添加
itemView.setOnClickListener(this);
答案 1 :(得分:1)
请添加以下行“ itemView.setOnClickListener(this);”在“ ArtistViewHolder”的构造函数中
public ArtistViewHolder(View itemView){
super(itemView);
artistImage = itemView.findViewById(R.id.artist_image);
artistName = itemView.findViewById(R.id.artist_name);
artistGenre = itemView.findViewById(R.id.artist_genre);
concertLocation = itemView.findViewById(R.id.concert_location);
concertDate = itemView.findViewById(R.id.concert_date);
itemView.setOnClickListener(this);
};