Activity不显示RecyclerView - 数据绑定

时间:2016-11-05 18:17:33

标签: android data-binding android-recyclerview android-databinding

我尝试使用RecyclerView进行数据绑定,但是我的main_activity没有显示我的Objects的RecyclerView内容。我不知道故障点在哪里。 谢谢你的任何建议。

这是我的简单代码

类:用户

public class User {
private String name;
private int age;

public User(String name, int age) {
    this.name = name;
    this.age = age;
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}
}

class:UserAdapter

public class UserAdapter extends   RecyclerView.Adapter<RecyclerView.ViewHolder> {
private Activity host;
private ArrayList<User> items;

public UserAdapter(Activity activity, ArrayList<User> items) {
    this.host = activity;
    this.items = items;
    Log.i("UserAdapterName", items.get(0).getName().toString()); //log shows Gordon

}

@Override
public UserAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // inflate layout and retrieve binding
    ListItemsBinding binding = DataBindingUtil.inflate(host.getLayoutInflater(),
            R.layout.list_items, parent, false);;

    return new ItemViewHolder(binding);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    User item = items.get(position);

    ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
    itemViewHolder.bindItem(item);
}

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

static class ItemViewHolder extends RecyclerView.ViewHolder {
    ListItemsBinding binding;

    ItemViewHolder(ListItemsBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    void bindItem(User item) {
        binding.setUser(item);
        binding.executePendingBindings();
    }
}
}

class:MainActivity

public class MainActivity extends AppCompatActivity {
private ArrayList<User> userList = new ArrayList<User>();
private ActivityMainBinding binding;
private UserAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    fillData();
    adapter = new UserAdapter(MainActivity.this,userList);
    binding.recycler.setAdapter(adapter);
}


private void fillData() {
    userList.add(new User("Gordon", 26));
    userList.add(new User("Dick", 34));
    userList.add(new User("Ivan",20));
}
}

layout:activity_main

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Activity Text"/>

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

</RelativeLayout>

layout:list_items

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

<data>

    <variable
        name="user"
        type="de.bp2go.recgo.User" />

</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">



            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.name}"
                android:textAppearance="?android:attr/textAppearanceMedium"
                android:textColor="@android:color/black"
                tools:text="Name" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{user.age}"
                android:textColor="@android:color/black"
                tools:text="Age" />

        </LinearLayout>
</layout>

2 个答案:

答案 0 :(得分:1)

对我而言,您似乎忘了为RecyclerView设置正确的LayoutManager。

垂直列表缺少行:

binding.recycler.setLayoutManager(new LinearLayoutManager(this));

答案 1 :(得分:0)

public class MainActivity extends AppCompatActivity {
private ArrayList<User> userList = new ArrayList<User>();
private ActivityMainBinding binding;
private UserAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    fillData();

         recycler = (RecyclerView) findViewById(R.id.recycler);


        LinearLayoutManager manager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recycler.setLayoutManager(manager);


    adapter = new UserAdapter(MainActivity.this,userList);
    recycler.setAdapter(adapter);
}


private void fillData() {
    userList.add(new User("Gordon", 26));
    userList.add(new User("Dick", 34));
    userList.add(new User("Ivan",20));
}
}

public class UserAdapter extends   RecyclerView.Adapter<ItemViewHolder.ViewHolder> {
private Activity host;
private ArrayList<User> items;
LayoutInflater inflator;
public UserAdapter(Activity activity, ArrayList<User> items) {
    this.host = activity;
    this.items = items;
    Log.i("UserAdapterName", items.get(0).getName().toString()); //log shows Gordon
 inflator=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

}

@Override
public UserAdapter.ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    // inflate layout and retrieve binding
    View view =inflator.inflate(R.layout.list_items, parent,false);


    return new ItemViewHolder(view);
}

@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
    User item = items.get(position);

    ItemViewHolder itemViewHolder = (ItemViewHolder)holder;
    itemViewHolder.bindItem(item);
}

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

static class ItemViewHolder extends RecyclerView.ViewHolder {
    ListItemsBinding binding;

    ItemViewHolder(ListItemsBinding binding) {
        super(binding.getRoot());
        this.binding = binding;
    }

    void bindItem(User item) {
        binding.setUser(item);
        binding.executePendingBindings();
    }
}

}