Android:RecyclerView不显示片段中的列表项

时间:2017-08-22 22:32:22

标签: android android-recyclerview fragment

有人可以帮我试试看我的RecyclerView吗?如果我不在片段中实现它,似乎。但是,当我尝试将其实现为片段时,我在(CarFront)中的其他XML代码与RecyclerView分开显示。

我在日志中收到此错误: “E / RecyclerView:没有连接适配器;跳过布局”

以下是我的代码,如果您需要任何其他信息或其他代码部分,请随时在下面发表评论。非常感谢你。

适配器:

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<ItemObject> itemList;
    private Context context;

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {

        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;

    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        //holder.countryName.setText(itemList.get(position).getName());
        holder.photo.setImageResource(itemList.get(position).getPhoto());


    }

    @Override
    public int getItemCount() {

        Log.e("Test", "" + this.itemList.size());
        return this.itemList.size();

    }


}

片段(欢迎):

public class Welcome extends AppCompatActivity {

    // Declaring variables
    private ViewPager viewPager;
    private ViewPagerAdapter viewPagerAdapter;
    private LinearLayout sectionLayout;
    private TextView[] sections;
    private int[] welcome_screen;
    private Button Skip, Next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Changing the window to fullscreen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);
        // Hiding the action bar to make it fullscreen
        getSupportActionBar().hide();

        // Setting the variables equal to their counter-part in the .xml
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        sectionLayout = (LinearLayout) findViewById(R.id.sections_layout);
        Skip = (Button) findViewById(R.id.skip);
        Next = (Button) findViewById(R.id.next);

        // Adding the various layouts to the welcome screen
        welcome_screen = new int[]{
                R.layout.activity_car_front,
                R.layout.activity_car_side};

        // adding sections
        SectionsDesign(0);

        viewPagerAdapter = new ViewPagerAdapter();
        viewPager.setAdapter(viewPagerAdapter);
        viewPager.addOnPageChangeListener(viewPagerPageChangeListener);

        //RecyclerView
        RecyclerView recyclerView = (RecyclerView)findViewById(R.id.front_recyclerview);

    }


    public  void nextclick(View v)
    {

        // Checks to see if it has reached the homescreen, if so proceed
        int current = getItem(1);
        if (current < welcome_screen.length) {
            // moving to the next section
            viewPager.setCurrentItem(current);
        } else {
            launchHomeScreen();
        }
    }

    ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            SectionsDesign(position);

            // When it reaches the last section, show launch button
            if (position == welcome_screen.length - 1) {
                // last page. make button text to GOT IT
                Next.setText("LAUNCH");
                Skip.setVisibility(View.GONE);
            } else {
                // Physical next button (blank for design)
                Next.setText("");
                Skip.setVisibility(View.VISIBLE);
            }
        }

        // Not using but need
        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {

        }
        @Override
        public void onPageScrollStateChanged(int arg0) {

        }
    };

    private void SectionsDesign(int currentPage) {
        // Getting how many sections there are
        sections = new TextView[welcome_screen.length];

        sectionLayout.removeAllViews();
        for (int i = 0; i < sections.length; i++) {
            sections[i] = new TextView(this);
            // Shows how many sections there are
            sections[i].setText(Html.fromHtml("&#8226;"));
            // Size of the sections slider (dot)
            sections[i].setTextSize(35);
            // For other sections that isn't the one you are on
            sections[i].setTextColor(getResources().getColor(R.color.section_inactive));
            sectionLayout.addView(sections[i]);
        }

        if (sections.length > 0)
            // Change colour to active
            sections[currentPage].setTextColor(getResources().getColor(R.color.section_active));
    }


    private int getItem(int i) {
        return viewPager.getCurrentItem() + i;
    }

    // Method for switching to the main screen
    private void launchHomeScreen() {
        startActivity(new Intent(this, Form.class));
        finish();
    }

    public class ViewPagerAdapter extends PagerAdapter {
        private LayoutInflater layoutInflater;


        public ViewPagerAdapter() {

        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View view = layoutInflater.inflate(welcome_screen[position], container, false);
            container.addView(view);

            return view;
        }

        @Override
        public boolean isViewFromObject(View view, Object obj) {
            return view == obj;
        }

        @Override
        // Number of sections
        public int getCount() {
            return welcome_screen.length;
        }


        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            View view = (View) object;
            container.removeView(view);
        }
    }

CarFront(我在片段中的一个观点):

public class CarFront extends AppCompatActivity {

    private LinearLayoutManager lLayout;
    public static ImageView fbumper;
    public static ImageView hood;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //remove title/fullscreen
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        //getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                //WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_car_front);
        setTitle(null);

        List<ItemObject> rowListItem = getAllItemList();
        lLayout = new LinearLayoutManager(CarFront.this);

        RecyclerView rView = (RecyclerView)findViewById(R.id.front_recyclerview);
        rView.setLayoutManager(lLayout);

        RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(CarFront.this, rowListItem);
        rView.setAdapter(rcAdapter);

        //Selected Images

        fbumper = (ImageView)findViewById(R.id.fbumperselected);
        hood = (ImageView)findViewById(R.id.hoodselected);




    }

    private List<ItemObject> getAllItemList(){

        List<ItemObject> allItems = new ArrayList<ItemObject>();

        allItems.add(new ItemObject("", R.drawable.hood_selected));

        //allItems.add(new ItemObject("", R.drawable.hood_card));


        return allItems;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder
    {

        public ViewHolder(View itemView) {
            super(itemView);
        }
    }



}

CarFront(XML):

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@color/background">

    <android.support.v7.widget.Toolbar
        android:layout_width="fill_parent"
        android:layout_height="45dp"
        android:background="@drawable/top_logo">

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

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="200dp">

        <ImageView
            android:id="@+id/carfront"
            android:layout_width="368dp"
            android:layout_height="200dp"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/carfront"
            tools:layout_editor_absoluteY="50dp"
            tools:layout_editor_absoluteX="8dp" />

        <ImageView
            android:id="@+id/hoodselected"
            android:layout_width="368dp"
            android:layout_height="200dp"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/hood_selected"
            tools:layout_editor_absoluteY="50dp"
            tools:layout_editor_absoluteX="8dp"
            android:visibility="invisible"/>


        <ImageView
            android:id="@+id/fbumperselected"
            android:layout_width="368dp"
            android:layout_height="200dp"
            android:scaleType="fitStart"
            app:srcCompat="@drawable/fbumper_selected"
            tools:layout_editor_absoluteY="0dp"
            tools:layout_editor_absoluteX="8dp"
            android:visibility="invisible"/>

    </android.support.constraint.ConstraintLayout>



        <android.support.v7.widget.RecyclerView
            android:id="@+id/front_recyclerview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="none" />


</LinearLayout>

RecyclerViewHolder:

    public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnTouchListener{

    //public TextView countryName;
    public ImageView photo;
    public ImageView fbumper;

    public RecyclerViewHolders(View itemView) {
        super(itemView);
        itemView.setOnTouchListener(this);
        photo = (ImageView)itemView.findViewById(R.id.cardview);
    }

    @Override
    //make a gesture detector
    //make a nice clean graphic after they tap on repair, telling the user how it works
    //pointer = multitouch
    public boolean onTouch(View view, MotionEvent event)
    {
        switch (event.getAction() & MotionEvent.ACTION_MASK)
        {
            case MotionEvent.ACTION_DOWN:
                actionDown();
                break;

            case MotionEvent.ACTION_POINTER_UP:
                actionUp();
                break;
        }

        return true;
    }

    // Method for Down Press
    public void actionDown()
    {
        switch (getAdapterPosition())
        {
            case 0:

                CarFront.fbumper.setVisibility(View.VISIBLE);
                break;

            case 1:
        }

    }
    //Method for Up Press
    public void actionUp()
    {
        switch (getAdapterPosition())
        {
            case 0:

                CarFront.fbumper.setVisibility(View.INVISIBLE);
                //break;

            case 1:
        }
    }


}

(我在尝试格式化和解释我的问题时很垃圾。但是,基本上我的RecyclerView没有显示在我的片段中。它是为了显示Cardview / s)

1 个答案:

答案 0 :(得分:5)

好的,首先,你的片段结构应该更像下面的结构。在您的onCreateView中,只需夸大布局,然后在onViewCreated中填充/填充您的信息。

我们也使用newInstance,因为这是创建新片段的标准方法。

注意,我使用的是android.support.v4.app.Fragment,而不是较旧的Fragment

public class CarFrontFragment extends Fragment {

    public ImageView bumper; // Do not use static views.
    public ImageView hood;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_car_front, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Bind your views.
        RecyclerView list = (RecyclerView) view.findViewById(R.id.front_recyclerview);
        bumper = (ImageView) view.findViewById(R.id.fbumperselected);
        hood = (ImageView) view.findViewById(R.id.hoodselected);

        // Create your layout manager.
        LinearLayoutManager layout = new LinearLayoutManager(getContext());
        list.setLayoutManager(layout);

        // Fetch your items.
        List<ItemObject> rowListItem = getAllItemList();

        // Set your adapter
        RecyclerViewAdapter adapter = new RecyclerViewAdapter(getContext(), rowListItem);
        list.setAdapter(adapter);

    }

    // Creating a new Fragment through newInstance.
    public static CarFrontFragment newInstance() {
        return new CarFrontFragment();
    }


    // Fetching items, passing in the View they will control.
    private List<ItemObject> getAllItemList(){
        List<ItemObject> allItems = new ArrayList<ItemObject>();

        allItems.add(new ItemObject("", R.drawable.hood_selected), hood);
        allItems.add(new ItemObject("", R.drawable.bumped_selected), bumper);

        return allItems;
    }
}

现在,对于包含这些的适配器,您需要FragmentStatePagerAdapter。所有这一切都将位置绑定到您选择的Fragment

public class PagerAdapter extends FragmentStatePagerAdapter {

    public static final int NUM_OF_PAGES = 2;
    private static final int PAGE_WELCOME = 0;
    private static final int PAGE_CAR_FRONT = 1;

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
            case PAGE_WELCOME:
                // Return a new instance of your Fragment.

            case PAGE_CAR_FRONT:
                return CarFrontFragment.newInstance();
        }
        return null;
    }

    @Override
    public int getCount() {
        return NUM_OF_PAGES;
    }
}

在适配器中,我们要做的就是通过ItemObject控制的View

public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewHolders> {

    private List<ItemObject> itemList;
    private Context context;

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) {
        this.itemList = itemList;
        this.context = context;
    }

    @Override
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
        View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_list, null);
        RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
        return rcv;

    }

    @Override
    public void onBindViewHolder(RecyclerViewHolders holder, int position) {
        holder.photo.setImageResource(itemList.get(position).getPhoto());
        // Telling the holder about the View.
        holder.view = itemList.get(position).getView();
    }

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

现在,在RecyclerViewHolders中,我们会使用onTouch切换视图的可见性。

public class RecyclerViewHolders extends RecyclerView.ViewHolder implements View.OnTouchListener {

    ImageView photo;
    public View view;

    public RecyclerViewHolders(View itemView) {
        super(itemView);
        itemView.setOnTouchListener(this);
        photo = (ImageView) itemView.findViewById(R.id.cardview);
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                this.view.setVisibility(View.VISIBLE);
                break;

            case MotionEvent.ACTION_POINTER_UP:
                this.view.setVisibility(View.GONE);
                break;
        }

        return true;
    }

}