2

我的视频问题

我在 RecyclerView 中选择项目时遇到问题。当我单击时,项目出现紫色覆盖,但是当我滚动到另一个屏幕时,它会出现另一个项目,当我向后滚动时,旧项目覆盖消失。我不知道发生了什么:(如何解决它???

RecyclerView_Adapter

package basic.zhenyuan0502.nguyenlt.armstore;
import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.support.percent.PercentLayoutHelper;
import android.support.percent.PercentRelativeLayout;
import android.support.v17.leanback.widget.BaseCardView;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.util.SparseBooleanArray;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

/**
 * Created by Heisenberg on 17/04/2016.
 */
public class RecyclerView_Adapter<VH extends RecyclerView_Adapter.ViewHolder> extends RecyclerView.Adapter<VH> {
    ArrayList<Item> arrayList = new ArrayList<Item>();
    private Context context;
    private int focusedItem = 0;

    public RecyclerView_Adapter(Context context, ArrayList<Item> arrayList) {
        this.context = context;
        this.arrayList = arrayList;

    }

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

    @Override
    public void onAttachedToRecyclerView(final RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        recyclerView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                RecyclerView.LayoutManager lm = recyclerView.getLayoutManager();

                // Return false if scrolled to the bounds and allow focus to move off the list
                if (event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
                        return tryMoveSelection(lm, 1);
                    } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                        return tryMoveSelection(lm, -1);
                    }
                }
                return false;
            }
        });
    }

    private boolean tryMoveSelection(RecyclerView.LayoutManager lm, int direction) {
        int tryFocusItem = focusedItem + direction;
        if (tryFocusItem >= 0 && tryFocusItem < getItemCount()) {
            notifyItemChanged(focusedItem);
            focusedItem = tryFocusItem;
            notifyItemChanged(focusedItem);
            lm.scrollToPosition(focusedItem);
            return true;
        }
        return false;
    }

    @Override
    public void onBindViewHolder(final VH vHolder, int position) {

        final Item item = arrayList.get(position);
        vHolder.ivItem.setImageResource(item.getIvItem());
        vHolder.tvPrice.setText(item.getTvPrice());
        vHolder.tvName.setText(item.getTvName());

        vHolder.setItemClickListener(new ItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                vHolder.itemView.setSelected(focusedItem == position);
                //notifyItemChanged(focusedItem);
                focusedItem = position;
                vHolder.selected_overlay.setVisibility(View.VISIBLE);
                //  notifyItemChanged(focusedItem);
            }
        });
    }

    @Override
    public VH onCreateViewHolder(ViewGroup viewGroup, int viewType) {

        LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
        ViewGroup mainGroup = (ViewGroup) inflater.inflate(
                R.layout.themeblue_item_recyclerview, viewGroup, false);
        ViewHolder viewHolder = new ViewHolder(mainGroup);

        return (VH) viewHolder;
    }

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public ItemClickListener itemClickListener;
        public TextView tvPrice, tvName;
        public ImageView ivItem;
        public LinearLayout llItem;
        public CardView cvItem;
        public View selected_overlay;


        public ViewHolder(View convertView) {
            super(convertView);
            ivItem = (ImageView) convertView.findViewById(R.id.ivItem);
            tvPrice = (TextView) convertView.findViewById(R.id.tvPrice);
            tvName = (TextView) convertView.findViewById(R.id.tvName);
            llItem = (LinearLayout) convertView.findViewById(R.id.llItem);
            cvItem = (CardView) convertView.findViewById(R.id.cvItem);
            selected_overlay = (View) convertView.findViewById(R.id.selected_overlay);
            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            this.itemClickListener.onItemClick(v, getLayoutPosition());
        }

        public void setItemClickListener(ItemClickListener itemClickListener) {
            this.itemClickListener = itemClickListener;
        }
    }


}

themeblue_item_recyclerview

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cvItem"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"


    android:foreground="?android:attr/selectableItemBackground"

    card_view:cardBackgroundColor="@android:color/transparent">

    <ImageView
        android:id="@+id/ivItem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="fitXY" />

    <LinearLayout
        android:id="@+id/llItem"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_vertical"
        android:background="#50000000"
        android:orientation="vertical">

        <TextView

            android:id="@+id/tvName"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Name"
            android:textColor="#FFFFFF"
            android:textSize="12sp"
            android:textStyle="bold" />

        <TextView
            android:id="@+id/tvPrice"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:gravity="center"
            android:text="Price"
            android:textColor="#FFFF26"
            android:textSize="12sp"
            android:textStyle="bold" />
    </LinearLayout>
    <View
        android:id="@+id/selected_overlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#88673AB7"
        android:visibility="invisible" />

</android.support.v7.widget.CardView>

OrderActivity:您只需专注于 runOnUiThread 和 lvDetailMiddle

package basic.zhenyuan0502.nguyenlt.armstore;


import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.percent.PercentLayoutHelper;
import android.support.percent.PercentRelativeLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.ActionMode;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.GestureDetector;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;

import com.sileria.android.Kit;
import com.sileria.android.view.HorzListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;

public class OrderActivity extends AppCompatActivity {
    private static final int SWIPE_MIN_DISTANCE = 120;
    private static final int SWIPE_MAX_OFF_PATH = 250;
    private static final int SWIPE_THRESHOLD_VELOCITY = 200;
    private String IP = "192.168.1.5";
    private String PORT_SSL = "8080";
    private final String LOGIN_URL = "http://" + IP + ":" + PORT_SSL + "/armstore/json.php";
    //    private ArrayList<TwoItem> arrayList = new ArrayList<>();
    private ArrayList<Item> arrayList = new ArrayList<>();
    private GestureDetector gestureDetector;
    private View.OnTouchListener gestureListener;
    RecyclerView lvDetailMiddle;
    RecyclerView_Adapter adapter;


    private static String docNoiDung_Tu_URL(String theUrl) {
        StringBuilder content = new StringBuilder();

        try {
            URL url = new URL(theUrl);
            URLConnection urlConnection = url.openConnection();
        wrap the urlconnection in a bufferedreader
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                content.append(line + "\n");
            }
            bufferedReader.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return content.toString();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        Kit.init(this);
        super.onCreate(savedInstanceState);

        //Set full screen open
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        getSupportActionBar().hide(); //Use to hide ActionBar with theme AppCompat
        //Set full screen close
        setContentView(R.layout.activity_order_oneline_item);

        //ListView TopBar
        final HorzListView lvTopBar = (HorzListView) findViewById(R.id.lvTopBar);
        String[] arrTxt = {"Món 1", "Món 2", "Món 3", "Món 4",
                "Món 5", "Món 6", "Món 7", "Món 8", "Món 9", "Món 10"};

        ArrayAdapter<String> adapterStringlvTopBar = new ArrayAdapter<>(this,
                R.layout.themeblue_menu_horzlistview, arrTxt);

        lvTopBar.setAdapter(adapterStringlvTopBar);

        //ListView BottomBar
        final HorzListView lvBottomBar = (HorzListView) findViewById(R.id.lvBottomBar);
        ArrayAdapter<String> adapterStringlvBottomBar = new ArrayAdapter<>(this,
                R.layout.themeblue_menu_horzlistview, arrTxt);

        lvBottomBar.setAdapter(adapterStringlvBottomBar);

        lvTopBar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                lvBottomBar.getSelectedView().setSelected(false);
                Toast.makeText(getApplicationContext(), "Chọn " + position, Toast.LENGTH_SHORT).show();
            }

        });


        lvBottomBar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                lvTopBar.getSelectedView().setSelected(false);
                Toast.makeText(getApplicationContext(), "Chọn " + position, Toast.LENGTH_SHORT).show();
            }
        });

        //EditText Search
        final EditText etSearch = (EditText) findViewById(R.id.etSearch);
        Button btnDelete = (Button) findViewById(R.id.btnDelete);
        btnDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                etSearch.setText("");
            }
        });

        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                new readJSON().execute(LOGIN_URL);
            }
        });

        lvDetailMiddle = (RecyclerView)
                findViewById(R.id.lvMiddle);
        lvDetailMiddle.setHasFixedSize(true);
        adapter = new RecyclerView_Adapter(OrderActivity.this, arrayList);
        adapter.notifyDataSetChanged();
        lvDetailMiddle.setAdapter(adapter);

        lvDetailMiddle.setItemAnimator(new DefaultItemAnimator());
        lvDetailMiddle
                .setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.HORIZONTAL));


        ImageButton ibtnDetail = (ImageButton) findViewById(R.id.ibtnDetail);
        ibtnDetail.setOnClickListener(new View.OnClickListener()

                                      {
                                          @Override
                                          public void onClick(View v) {
                                              Intent intent = new Intent(getApplication(), ProcessActivity.class);
                                              startActivity(intent);
                                          }
                                      }

        );
        gestureDetector = new GestureDetector(this, new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                return gestureDetector.onTouchEvent(event);
            }
        };

        Kit.destroy();
    }

    @Override
    public void onBackPressed() {
        OrderActivity.this.finish();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class readJSON extends AsyncTask<String, Integer, String> {


        @Override
        protected String doInBackground(String... params) {
            return docNoiDung_Tu_URL(params[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            //ArrayList<String> arrProduct = new ArrayList<String>();
            try {
                JSONArray mang = new JSONArray(s);

                for (int i = 0 ; i < mang.length(); ++i) {
                    JSONObject product = mang.getJSONObject(i);
                    arrayList.add(new Item(product.getString("name"),product.getString("price"),R.drawable.themeblue_logo_store_small));
                }

            } catch (JSONException e) {
                e.printStackTrace();

            }

        }
    }

    class MyGestureDetector extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(OrderActivity.this, "Đang cuộn <-", Toast.LENGTH_SHORT).show();
                } else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                    Toast.makeText(OrderActivity.this, "Đang cuộn ->", Toast.LENGTH_SHORT).show();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }

        @Override
        public boolean onDown(MotionEvent e) {
            return true;
        }

    }


}
4

2 回答 2

1

而不是在您的 onClick 方法中使用getLayoutPosition()尝试使用。getAdapterPosition()

于 2016-04-27T03:12:20.760 回答
-1

尝试在您的vHolder.itemView.setSelected(focusedItem == position);内部添加onBindViewHolderRecyclerViewAdapter

...
@Override
public void onBindViewHolder(final VH vHolder, int position) {
    ...
    vHolder.itemView.setSelected(focusedItem == position); // add it here for hightlight or unhightlight your RecycleViewCell when it recreate

    vHolder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onItemClick(View v, int position) {
            vHolder.itemView.setSelected(focusedItem == position);
            ...
            focusedItem = position;
            ...
        }
    });
}
于 2016-04-27T02:46:07.620 回答