Recyclerview项目消失

时间:2017-01-05 13:00:12

标签: android

我在recyclerview中以横向格式实施了layoutmanager android studio。一切顺利,直到安卓工作室崩溃。我设法恢复了我的所有代码,但这次每当我滚动时,我的textview项目就会消失。我尝试使用不同的API版本重建,但问题仍然存在。这是我的代码:

MainActivity

public class MainActivity extends AppCompatActivity {

    public static final String LOGTAG="kwik";
    SalesDataSource salesDS;
    SalesAdapter adapter;
    Sales sales;
    List<Sales> transaction;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the ViewPager and set it's PagerAdapter so that it can display items
        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
        viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

        // Give the PagerSlidingTabStrip the ViewPager
        PagerSlidingTabStrip tabsStrip = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        // Attach the view pager to the tab strip
        tabsStrip.setViewPager(viewPager);
    }
}

MovieAdapter

public class MovieAdapter extends RecyclerView.Adapter<MovieAdapter.MyViewHolder>{

    private List<MenuItem> menuItemList;

    public class MyViewHolder extends RecyclerView.ViewHolder {
        public TextView menuItem;

        public MyViewHolder(View view) {
            super(view);
            menuItem = (TextView) view.findViewById(R.id.tvMenuItem);

        }
    }

    public MovieAdapter(List<MenuItem> menuItemList) {
        this.menuItemList = menuItemList;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.menu_item_row, parent, false);

        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        MenuItem menu = menuItemList.get(position);
        holder.menuItem.setText(menu.getMenuItem());

    }

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

}

1 个答案:

答案 0 :(得分:0)

尝试

@Override
public void onBindViewHolder(MyViewHolder holder, final int position) {
    MenuItem menu = menuItemList.get(position);
    holder.menuItem.setText(menu.getMenuItem());
}

final int position将提供无法更改textView内部的信息