您好我刚刚设置了这个RecyclerView并且无法找到错误的内容。提前致谢。我确定RecyclerView正在显示,虽然它们在主活动屏幕上是一个微弱的阴影,我可以拉下来拉起来。我认为问题可能围绕我的线活动的这一部分:
getSupportActionBar().setTitle(null);
recyclerView = (RecyclerView) findViewById(R.id.line_list);
adapter=new MyAdapter(getApplicationContext(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
活动代码;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import java.util.ArrayList;
import java.util.List;
public class Lines extends AppCompatActivity {
private RecyclerView recyclerView;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lines);
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_top);
setSupportActionBar(myToolbar);
getSupportActionBar().setTitle(null);
recyclerView = (RecyclerView) findViewById(R.id.line_list);
adapter=new MyAdapter(getApplicationContext(),getData());
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_item, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int i = item.getItemId();
switch (i) {
case R.id.action_search:
Intent s = new Intent(this, SearchActivity.class);
startActivity(s);
return true;
case R.id.action_add:
Intent a = new Intent(this, AddLine.class);
startActivity(a);
return true;
case R.id.action_wheel:
Intent w = new Intent(this, Settings.class);
startActivity(w);
return true;
}
return super.onOptionsItemSelected(item);
}
public static List<Information> getData(){
List<Information> data=new ArrayList<>();
int[] icons={R.drawable.ic_place_black_24dp,R.drawable.ic_place_black_24dp2,
R.drawable.ic_place_black_24dp3};
String[] titles={"Homework","Birthday","Index"};
for(int i=0;i<titles.length && i<icons.length;i++){
Information current=new Information();
current.iconId=icons[i];
current.title=titles[i];
data.add(current);
}
return data;
}
}
.xml代码:
<LinearLayout 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:orientation="vertical"
tools:context=".Lines">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_top"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:minHeight="?attr/actionBarSize">
<TextView
android:id="@+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="LINES"
android:textColor="#ffffff"
android:textSize="25sp" />
</android.support.v7.widget.Toolbar>
<android.support.v7.widget.RecyclerView
android:id="@+id/line_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>
</LinearLayout>
行元素定义活动代码:
package com.example.patri.index01;
public class Information {
int iconId;
String title;
}
适配器活动代码:
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 MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
private LayoutInflater inflater;
List<Information> data= Collections.emptyList();
public MyAdapter(Context context, List<Information> data){
inflater=LayoutInflater.from(context);
this.data=data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view=inflater.inflate(R.layout.custom_row, parent,false);
MyViewHolder holder=new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Information current=data.get(position);
holder.title.setText(current.title);
holder.icon.setImageResource(current.iconId);
}
@Override
public int getItemCount() {
return 0;
}
class MyViewHolder extends RecyclerView.ViewHolder{
TextView title;
ImageView icon;
public MyViewHolder(View itemView) {
super(itemView);
title= (TextView)itemView.findViewById(R.id.listText);
icon= (ImageView) itemView.findViewById(R.id.listIcon);
}
}
}
行.xml代码:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/listIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_place_black_24dp"
android:padding="8dp"/>
<TextView
android:id="@+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dummy text"
android:padding="8dp"
android:textColor="#000000"/>
</LinearLayout>
答案 0 :(得分:1)
您的项目数为0
。
@Override
public int getItemCount() {
return 0;
}
您应该返回data.size()
你应该修复你的row.xml
,以便它的高度只有它所需要的大小:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="wrap_content">
答案 1 :(得分:0)
将row.xml中的linearlayout的layout_height设置为wrap_content而不是match_parent。这就是为什么它会填满整个屏幕。