我想要一個CheckBox(這是上面的RecyclerView)作爲QuickReturn。簡單的想法是在向下滾動時隱藏複選框,當滾動並使其可見時。 (如在https://github.com/dbleicher/recyclerview-grid-quickreturn。)QuickReturn與RecyclerView
而且,它的作品,但我滾動時有一個奇怪的閃爍。
對此有何看法?
MainActivity.java:
public class MainActivity extends ActionBarActivity {
private CheckBox mCheckbox;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCheckbox = (CheckBox) findViewById(R.id.checkbox);
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recview);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// Simple check if moved vertically.
// React to scrolls of a minimum amount (3, in this case)
if (dy > 3) {
if (mCheckbox.getVisibility() == View.VISIBLE) {
mCheckbox.setVisibility(View.GONE);
}
} else if (dy < -3) {
if (mCheckbox.getVisibility() == View.GONE) {
mCheckbox.setVisibility(View.VISIBLE);
}
}
}
});
List<String> dataset = Arrays.asList("one", "two", "three", "four", "five");
CustomAdapter adapter = new CustomAdapter(dataset);
recyclerView.setAdapter(adapter);
}
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
private List<String> mDataset;
public CustomAdapter(List<String> dataset) {
mDataset = dataset;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup arg0, int arg1) {
View v = LayoutInflater.from(arg0.getContext())
.inflate(R.layout.address_card, arg0, false);
return new ViewHolder(v);
}
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.mText.setText(mDataset.get(position));
}
@Override
public int getItemCount() {
return mDataset.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView mText;
public ViewHolder(View v) {
super(v);
mText = (TextView) v.findViewById(R.id.cardText);
}
}
}
}
activity_main.xml中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<CheckBox
android:id="@+id/checkbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check me" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" />
</LinearLayout>
adress_card.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:elevation="@dimen/cardview_default_elevation"
card_view:cardCornerRadius="@dimen/cardview_default_radius" >
<TextView
android:id="@+id/cardText"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:text="TextView" />
</android.support.v7.widget.CardView>
我似乎無法重新創建你提到的閃爍。什麼,正在閃爍?複選框或房車中的物品?如果它的RV中的項目(在滾動上口吃?)請發佈您的ViewHolder和onBind方法。如果其複選框閃爍,請仔細檢查複選框的高度計算,並顯示佈局XML。 – MojoTosh
嗯,閃爍是針對CheckBox以及RecyclerView中的CardView。它看起來有點像CheckBox從「VISIBLE」到「GONE」並返回永久區域。 我加了完整的代碼... –
你看過這個庫https://github.com/lawloretienne/QuickReturn? – toobsco42