使用ToolBar的RecyclerView

时间:2015-08-17 06:29:40

标签: java android android-layout android-fragments android-studio

我是材料设计的新手,每当我尝试使用RecyclerView时,每件事都会出错......

任何人都可以知道这是什么问题吗?

主要活动类

document.body.innerHTML = '<p>Hello</p>'

主要活动布局

 import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuItem;

 public class MainActivity extends ActionBarActivity {

    Toolbar toolbar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar= (Toolbar) findViewById(R.id.toool);
        setSupportActionBar(toolbar);
    }

    @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);
    }
 }  

Recycler片段

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <include
        android:id="@+id/toool"
        layout="@layout/app_tool" />

    <fragment
        android:id="@+id/fragment"
        android:name="com.example.hothyfa.recyclerandmore.Recycler"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/toool"
        android:layout_centerHorizontal="true"
        tools:layout="@layout/rere" />

</RelativeLayout>

Recycler class

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recye"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

RecyclerView Adapter类

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;
import java.util.List;

public class Recycler extends Fragment {

    private RecyclerView recyclerView;

    private MyRecyeAdapter adapter;
    private View view;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.rere, container, false);

        recyclerView = (RecyclerView) getActivity().findViewById(R.id.recye);

        adapter = new MyRecyeAdapter(getActivity(), getData());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        return view;
    }

    public static List<info> getData() {

        List<info> data = new ArrayList<>();
        int photos[] = { R.drawable.images, R.drawable.dreamsmal, R.drawable.images, R.drawable.dreamsmal };
        String users[] = { "Hothyfa", "Marwan", "Jamal", "Alaa" };

        for (int i = 0; i < photos.length && i < users.length; i++) {

            info current = new info();
            current.iconId = photos[i];
            current.Names = users[i];
            data.add(current);

        }
        return data;

    }

}

自定义行布局(对于RecyclerView)

import android.content.Context;

import android.support.v7.widget.RecyclerView;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ImageView;

import android.widget.TextView;

import java.util.Collections;

import java.util.List;

public class MyRecyeAdapter extends RecyclerView.Adapter<MyRecyeAdapter.myholder> {

    List<info> data = Collections.emptyList();
    private LayoutInflater inflater;

    public MyRecyeAdapter(Context context, List<info> data) {

        inflater = LayoutInflater.from(context);
        this.data = data;

    }

    @Override
    public myholder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.custom, parent, false);
        myholder holder = new myholder(view);

        return holder;
    }

    @Override
    public void onBindViewHolder(myholder holder, int position) {

        info current = data.get(position);

        holder.imageProfile.setImageResource(current.iconId);

        holder.UsersName.setText(current.Names);
    }

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

    public class myholder extends RecyclerView.ViewHolder {

        ImageView imageProfile;
        TextView UsersName;

        public myholder(View itemView) {

            super(itemView);
            imageProfile = (ImageView) itemView.findViewById(R.id.imageView);
            UsersName = (TextView) itemView.findViewById(R.id.textView);

        }
    }

}

和Recycler的数据类

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal" >

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="8dp"
            android:src="@drawable/images" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:padding="8dp"
            android:text="Large Text" />

    </LinearLayout>

工具栏布局

public class info {

    int iconId;

    String Names;

}

注意:我已将其放入build.gradle

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primarycolor" >  
    </android.support.v7.widget.Toolbar>

并重建应用。

错误:

compile 'com.android.support:recyclerview-v7:22.2.1'

4 个答案:

答案 0 :(得分:0)

  try to edit this     

   <fragment
     android:id="@+id/fragment"
     android:name="com.example.hothyfa.recyclerandmore.Recycler"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/toool"
     android:layout_centerHorizontal="true"
  />

答案 1 :(得分:0)

好的,我在onCreateView Recycler

recyclerView= (RecyclerView) getActivity().findViewById(R.id.recye); 中找到了问题
recyclerView= (RecyclerView) view.findViewById(R.id.recye);

将其更改为:

handleChange(name, e) {
    const change = {};
    change[name] = e.target.value;
    this.setState(change);
    console.log('---state:--', this.state);
  }

答案 2 :(得分:0)

    <RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" > 
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:minHeight="?attr/actionBarSize"
    android:background="#2196F3"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<fragment
    android:id="@+id/fragment"
    android:name="com.example.hothyfa.recyclerandmore.Recycler"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toool"
    android:layout_centerHorizontal="true"
    tools:layout="@layout/rere" />
</RelativeLayout>

答案 3 :(得分:0)

试试这个:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingTop="?attr/actionBarSize"
        android:clipToPadding="false"/>

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"/>

</FrameLayout>

您可以看到这两个链接How to hide/show toolbar in response to scrolling on AndroidRecyclerview with Cards example in Android with AppCompat (V7)

了解任何问题