我使用 RecyclerView 来显示和排序级别。因为 ListView 速度很慢我没有使用listView并使用RecyclerView。
因此,我需要一些ListView
方法,此方法包含SetScrollPosition
和SetOnItemSelect
。
我的RecyclerView适配器代码:
public class levels_card_adapter extends
RecyclerView.Adapter<levels_card_adapter.ViewHolder> {
private static String[] adapter_levels_number;
public levels_card_adapter(String[] adapter_levels_num_data) {
adapter_levels_number = adapter_levels_num_data;
}
@Override
public levels_card_adapter.ViewHolder onCreateViewHolder(ViewGroup parent,
int viewType) {
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.levels_recycler_layout, null);
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
viewHolder.adapter_levels_text.setText(adapter_levels_number[position].toString());
viewHolder.adapter_levels_version = adapter_levels_number[position];
}
@Override
public int getItemCount() {
return adapter_levels_number.length;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
public TextView adapter_levels_text;
public String adapter_levels_version;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
adapter_levels_text = (TextView) itemLayoutView
.findViewById(R.id.level_thumnail_text);
itemLayoutView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent question_page_intent = new Intent(v.getContext(), com.tellfa.braingame.activities.question_Page.class);
v.getContext().startActivity(question_page_intent);
}
});
}
}
活动代码:
public class play_Page extends ActionBarActivity {
private RecyclerView play_recycler_levels;
private RecyclerView.Adapter play_adapter_levels;
private TextView play_header_text;
private tellfa_textView play_thumbnail_levels_num, play_time_level_num;
private DiscreteSeekBar play_scroll_seekbar;
private com.github.jorgecastilloprz.FABProgressCircle fb;
private Context context;
private ListView play_list_levels;
public static String[] levels_number = {"1", "2", "3", "4", "5", "6", "7", "8", "9",
"10", "11", "12", "13", "14", "15", "16"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.play__page);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
///////////// Font Style
Typeface yekan_font = Typeface.createFromAsset(getAssets(), "fonts/yekan.ttf");
play_header_text = (TextView) findViewById(R.id.header_play_text);
play_header_text.setTypeface(yekan_font);
/////////////
play_thumbnail_levels_num = (tellfa_textView) findViewById(R.id.level_thumnail_text);
play_time_level_num = (tellfa_textView) findViewById(R.id.time_levels_num);
///////////// SeekBar
play_scroll_seekbar = (DiscreteSeekBar) findViewById(R.id.scroll_seekbar_play);
///////////// Recycler View
play_recycler_levels = (RecyclerView) findViewById(R.id.levels_recycler);
play_recycler_levels.setHasFixedSize(true);
play_recycler_levels.setLayoutManager(new LinearLayoutManager(this));
play_adapter_levels = new levels_card_adapter(levels_number);
play_recycler_levels.setAdapter(play_adapter_levels);
////////////
/*
//////////// List
com.tellfa.braingame.adapters.levels_list_adapter adapter=new com.tellfa.braingame.adapters.levels_list_adapter
(this, levels_number);
play_list_levels=(ListView)findViewById(R.id.list);
play_list_levels.setAdapter(adapter);
play_list_levels.setScrollingCacheEnabled(true);
///////////
*/
FrameLayout containerLayout = (FrameLayout) findViewById(R.id.rel_id);
HeaderAnimator animator = new HeaderAnimator(this);
StikkyHeaderBuilder.stickTo(play_recycler_levels)
.setHeader(R.id.header, containerLayout)
.minHeightHeader(250)
.animator(animator)
.build();
///////////// Refresh
final SwipeRefreshLayout play_refresh = (SwipeRefreshLayout) findViewById(R.id.refresh_play_layout);
play_refresh.setColorSchemeResources(R.color.refresh1, R.color.refresh2, R.color.refresh3, R.color.refresh4, R.color.refresh5);
play_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
play_refresh.setRefreshing(true);
}
});
}
我该如何制作这种方法? 这对我来说非常重要
答案 0 :(得分:0)
setScrollX()和RecyclerView.OnItemTouchListener呢?
public void setScrollX(int value)
设置视图的水平滚动位置。
RecyclerView.OnItemTouchListener
OnItemTouchListener允许应用程序拦截触摸 RecyclerView的视图层次结构级别正在进行的事件 在这些触摸事件被考虑用于RecyclerView自己之前 滚动行为。
答案 1 :(得分:0)
我已经实现了类似的东西,使用RecyclerView滚动的唯一方法是手动计算位置并使用您正在使用的LayoutManager进行滚动。
我在我的适配器中使用了这两种方法来实现这一点,如果你发现它可以帮助你使用它:
public void scrollToPosition(int positionToScrollTo) {
LinearLayoutManager layoutManager = null;
try {
layoutManager = (LinearLayoutManager) mRecyclerView.getLayoutManager();
} catch (ClassCastException e) {
log.d("You should pass a recyclerview with a layoutmanayer" +
"that inherits from LinearLayoutManager");
return;
}
int firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition();
View firstVisibleItemView = layoutManager.findViewByPosition(firstVisibleItemPosition);
int firstViewHeight = firstVisibleItemView.getHeight();
int scrollOffsetVertical = firstViewHeight * positionToScrollTo;
mRecyclerView.scrollBy(0, scrollOffsetVertical);
}
public void smoothScrollToPosition(int positionToSCroll) {
for (int i = 0;i < positionToSCroll; i++) {
scrollToPosition(i);
}
}
应该有更好的方法来做到这一点,但这对我有用,希望它有所帮助,祝你好运......