RecyclerView onitemSelectedListener(可能重复),如何做?

问题描述 投票:0回答:2

我将代码从ListView升级到RecyclerView,但是我遇到了一个问题,找不到答案,我用了几天的时间在Google上搜索,但没有任何线索。只是对我感到陌生。

我设法添加OnclickListener巫婆效果很好。但是,如果我在Android TV上运行该应用程序,我将不知道该如何实现:

1-on滚动,显示滚动条在哪里:如更改项目背景

2-同时滚动返回项目网址,以便我可以将其发送给播放器

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context=".MainActivity">

<com.google.android.exoplayer2.ui.SimpleExoPlayerView
    android:id="@+id/playerView_step_video"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:use_controller="false"
    />

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:layout_width="200dp"
    android:layout_height="match_parent"
    android:background="#60000000"
    android:overScrollMode="always"
    />

activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/llItemView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:background="#80222222"
android:orientation="horizontal">

<LinearLayout android:id="@+id/th"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="3dip"
    android:layout_alignParentLeft="true"
    android:layout_marginRight="5dip"
    android:layout_alignParentStart="true"
    android:layout_marginEnd="5dip">

    <TextView
        android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:layout_marginRight="4dp"
        android:layout_gravity="center_vertical"
        android:textColor="#fff"
        android:textSize="12sp"
        android:layout_marginEnd="4dp" />
    <ImageView
        android:id="@+id/img_item"
        android:layout_width="50dip"
        android:layout_height="50dip"
        android:contentDescription="@string/app_name"
        android:scaleType="fitXY"
        android:src="@drawable/selection_band_overlay" />

    <TextView
        android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp"
        android:layout_marginLeft="4dp"
        android:layout_gravity="center_vertical"
        android:textColor="#fff"
        android:textSize="12sp"
        android:layout_marginStart="4dp" />

    <TextView
        android:id="@+id/tvUrl"
        android:layout_width="fill_parent"
        android:visibility="invisible"
        tools:visibility="invisible"
        android:layout_height="fill_parent" />

</LinearLayout>

item_custom.xml

public class ListAdapter extends RecyclerView.Adapter<ListAdapter.RecyclerViewHolder> {

private List<ItemAdapter> dataModelList;
private Context context;
private OnClickListener OnClickListener;
private int selected_position = 0;



class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
    TextView tvID;
    TextView tvName;
    TextView tvUrl;
    ImageView tvImg;
    RelativeLayout llItemView;
    OnClickListener OnclickListener;

        RecyclerViewHolder(View itemView, OnClickListener OnclickListener) {
            super(itemView);
            llItemView = itemView.findViewById(R.id.llItemView);
            tvID = itemView.findViewById(R.id.tvId);
            tvName = itemView.findViewById(R.id.tvName);
            tvUrl = itemView.findViewById(R.id.tvUrl);
            tvImg = itemView.findViewById(R.id.img_item);
            this.OnclickListener = OnclickListener;
            itemView.setOnClickListener(this);
        }

    @Override
    public void onClick(final View view) {
        if (getAdapterPosition() == RecyclerView.NO_POSITION) return;
        // Updating old as well as new positions
        notifyItemChanged(selected_position);
        selected_position = getAdapterPosition();
        notifyItemChanged(selected_position);
        TextView url = view.findViewById(R.id.tvUrl);
        OnClickListener.OnUrlClickListener((String) url.getText());
        //you will need to do something similar to this listener for all focusable things


    }
}





    //create constructor with list
    ListAdapter(List<ItemAdapter> dataModelList, OnClickListener OnClickListener) {
        this.dataModelList = dataModelList;
        this.OnClickListener = OnClickListener;
    }

    @NonNull
    @Override
    public RecyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        context = parent.getContext();
        RecyclerViewHolder viewHolder;
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_custom, parent, false);
        viewHolder = new RecyclerViewHolder(view, OnClickListener);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerViewHolder holder, final int position) {

        holder.tvID.setText(String.valueOf(dataModelList.get(position).getId()));
        holder.tvName.setText(dataModelList.get(position).getName());
        holder.tvUrl.setText(dataModelList.get(position).getUrl());
        Glide.with(context)
                .load(dataModelList.get(position).getImg())
                .apply(new RequestOptions()
                        .override(100, 100)
                        .transforms(new CenterCrop(), new FitCenter(),  new RoundedCorners(14)))
                .into(holder.tvImg);
        holder.itemView.setBackgroundColor(selected_position == position ? Color.GREEN : Color.TRANSPARENT);



    }

    @Override
    public long getItemId(int position) {

        return super.getItemId(position);
    }

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

public interface OnClickListener {
    void OnUrlClickListener(String url);
}


}

ListAdapter.class


public class MainActivity extends AppCompatActivity implements ListAdapter.OnClickListener {
private List<ItemAdapter> dataModelList;
RecyclerView recyclerView;
SimpleExoPlayer player;
LinearLayoutManager layoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActionBar actionBar = getSupportActionBar();
    actionBar.hide();
    // Instantiate the recyclerView
    recyclerView = findViewById(R.id.recyclerView);
    recyclerView.setHasFixedSize(true);
    layoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
    recyclerView.setLayoutManager(layoutManager);

    setData();
    setDataToRecyclerView();
    player = new SimpleExoPlayer.Builder(this).build();
    final PlayerView playerView = findViewById(R.id.playerView_step_video);
    playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
    playerView.setPlayer(player);
    player.setVideoScalingMode(C.VIDEO_SCALING_MODE_SCALE_TO_FIT_WITH_CROPPING);
    recyclerView.setVisibility(View.INVISIBLE);
    final RelativeLayout playerV = findViewById(R.id.parent);
    playerV.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            int visibility = recyclerView.getVisibility();
            if (visibility == 0){
                recyclerView.setVisibility(View.INVISIBLE);
            }else{
                recyclerView.setVisibility(View.VISIBLE);
            }
        }
    });
    recyclerView.setFocusable(true);
    recyclerView.setSelected(true);
}

private void setData() {
    String glaras = null;
    try {
        glaras = new glarab().execute().get();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    int i = 0;
    dataModelList = new ArrayList<>();
    try {

        String x = new JSONParser().execute().get();
        JSONObject jsonObject = new JSONObject(x);
        Iterator<String> keys = jsonObject.keys();
        while(keys.hasNext()) {
            i += 1;
            String number = "#" + i;
            String key = keys.next();
            JSONArray channels_pics = (JSONArray) jsonObject.get(key);
            JSONObject items = (JSONObject) channels_pics.get(0);
            String stream = (String) items.get("url");
            if(stream.contains("7777")){
                stream = stream+glaras;
            }
            if(stream.length() < 1){
                stream = "https://content.jwplatform.com/manifests/yp34SRmf.m3u8";
            }
            dataModelList.add(new ItemAdapter(number, key, stream, (String) items.get("logo")));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
private void setDataToRecyclerView() {
    ListAdapter adapter = new ListAdapter(dataModelList, this);
    recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    recyclerView.setAdapter(adapter);
}


public void OnUrlClickListener(String url) {
    final HttpDataSource.Factory dataSourceFactory = new DefaultHttpDataSourceFactory("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36");
    HlsMediaSource hlsMediaSource =
            new HlsMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse(url));
    player.prepare(hlsMediaSource);
    player.setPlayWhenReady(true);

}

@Override
public boolean dispatchKeyEvent(KeyEvent event) {
    final int keyCode = event.getKeyCode();
    if (event.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
        if (event.getAction() == KeyEvent.ACTION_UP ) {
            recyclerView.setVisibility(View.VISIBLE);         
            return true;
        }
    }


    //System.out.println(keyCode);
    if(keyCode == 22 || keyCode == 21 ){
        recyclerView.setVisibility(View.INVISIBLE);
    }
    return super.dispatchKeyEvent(event);
}

@Override
public void onBackPressed() {
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    MainActivity.this.finish();
    System.exit(0);
}

@Override
protected void onUserLeaveHint()
{
    Intent intent = new Intent(Intent.ACTION_MAIN);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    MainActivity.this.finish();
    System.exit(0);
}
}

所以再次使用D-pad我可以滚动,但是我不知道选择了哪个项目,改变背景会很不错,所以我知道哪个项目。

[以前,通过listView,我能够使用OnItemSelectedListener,更改选定项目的背景,获取字符串,只需逐个滚动项目即可。这就是我需要的RecyclerView。如果有人可以修改我的代码以使其正常工作,并说明那将是一件好事。非常感谢您]]

我将代码从ListView升级到RecyclerView,但是我遇到了一个问题,找不到答案,我用了几天的时间在Google上搜索,但没有任何线索。只是跟我忍耐吧。我设法添加...

android android-recyclerview television
2个回答
0
投票

您可以像以下代码一样输入Toast onBindViewHolder,也可以在单击时放置背景色。


0
投票

您必须在drawable中制作另一个xml文件,以突出显示如下所示的滚动条e-g

© www.soinside.com 2019 - 2024. All rights reserved.