我使用了其他答案中给出的技术,例如将依赖项的版本降级为24 ...和25.0.1.Did无效。我将RecyclerView的宽度和高度设置为match_parent。 以下是代码。我在博客应用程序中使用RecyclerView作为帖子标题和帖子描述:
package com.awani.pocketblog;
import android.content.ClipData;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private RecyclerView mBlogList;
private DatabaseReference mDatabase;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBlogList = (RecyclerView) findViewById(R.id.blog_list);
mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(new LinearLayoutManager(this));
mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Blog,BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>( Blog.class,
R.layout.blog_row,
BlogViewHolder.class,
mDatabase
) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, Blog model, int position) {
viewHolder.setTitle(model.getTitle());//model means Blog class over here
viewHolder.setDesc(model.getDesc());
}
};
mBlogList.setAdapter(firebaseRecyclerAdapter);
}
//holder for the recyclerView
public static class BlogViewHolder extends RecyclerView.ViewHolder{
View mView;
public BlogViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView) mView.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setDesc(String desc){
TextView post_description = (TextView) mView.findViewById(R.id.post_description);
post_description.setText(desc);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.action_add){
startActivity(new Intent(MainActivity.this,PostActivity.class));
}
return super.onOptionsItemSelected(item);
}
}
log cat语句可能导致此问题..
07-08 17:22:10.761 23308-23308/com.awani.pocketblog W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
这是activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout 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"
tools:context="com.awani.pocketblog.MainActivity">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/blog_list">
</android.support.v7.widget.RecyclerView>
</android.widget.RelativeLayout>
我的android studio版本是2.3.2,他的依赖是25.3.1 和firebase依赖项是10.0.1 firebase ui依赖1.1.1
答案 0 :(得分:0)
检查一下。我正在使用此代码打印已保存的新闻 您可以查看此信息以供参考 虽然我使用了片段,但您可以相应地为Activity更改代码。
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
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.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.student.newsapp.R;
import com.firebase.client.Firebase;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class show extends AppCompatActivity {
RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference myRef;
FirebaseAuth firebaseauth;
private FirebaseRecyclerAdapter<show_items, ShowDataViewHolder> mFirebaseAdapter;
private ProgressDialog progressDialog;
public show() {
// Required empty public constructor
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.rvforbookmarks);
Firebase.setAndroidContext(this);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait..");
progressDialog.show();
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Bookmarks");
firebaseDatabase = FirebaseDatabase.getInstance();
firebaseauth = FirebaseAuth.getInstance();
final String userid =firebaseauth.getCurrentUser().getUid();
// myRef = firebaseDatabase.getReference("User_Details");
myRef = FirebaseDatabase.getInstance().getReference("User_Details").child(userid);
recyclerView = (RecyclerView)findViewById(R.id.rv);
recyclerView.setLayoutManager(new LinearLayoutManager(show.this));
}
//View Holder For Recycler View
public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
private final TextView image_title ;
private final ImageView image_url;
private TextView imge_des;
private ImageView btn_delete;
private TextView nobookmark;
public ShowDataViewHolder(final View itemView)
{
super(itemView);
image_url = (ImageView) itemView.findViewById(R.id.fetch_image);
image_title = (TextView) itemView.findViewById(R.id.fetch_image_title);
imge_des = (TextView) itemView.findViewById(R.id.fetch_description);
btn_delete = (ImageView) itemView.findViewById(R.id.btn_delete);
nobookmark = (TextView) itemView.findViewById(R.id.nobookmark);
}
private void Image_Title(String title)
{
image_title.setText(title);
}
private void Image_URL(String title)
{
// image_url.setImageResource(R.drawable.loading);
Glide.with(itemView.getContext())
.load(title)
.crossFade()
.placeholder(R.drawable.loading)
.thumbnail(0.1f)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(image_url);
}
public void Image_Des(String title) {
imge_des.setText(title);
}
}
@Override
public void onStart() {
super.onStart();
mFirebaseAdapter = new FirebaseRecyclerAdapter<show_items, ShowDataViewHolder>(show_items.class, R.layout.showbookamrks, ShowDataViewHolder.class, myRef) {
public void populateViewHolder(final ShowDataViewHolder viewHolder, show_items model, final int position) {
viewHolder.Image_URL(model.getImage_URL());
viewHolder.Image_Title(model.getImage_Title());
viewHolder.Image_Des(model.getImage_des());
//OnClick Item
viewHolder.btn_delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(show.this);
builder.setMessage("Do you want to Delete this data ?").setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int selectedItems = position;
mFirebaseAdapter.getRef(selectedItems).removeValue();
mFirebaseAdapter.notifyItemRemoved(selectedItems);
recyclerView.invalidate();
onStart();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.setTitle("Confirm");
dialog.show();
}
});
progressDialog.dismiss();
}
};
recyclerView.setAdapter(mFirebaseAdapter);
}
}
希望它有所帮助 三江源
答案 1 :(得分:0)
Follow This Example Step by Step
public class MainActivity extends AppCompatActivity implements RecyclerViewClickListener {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_area_list);
recyclerView=(RecyclerView)findViewById(R.id.recycler_view1);
populateRecyclerView();
recyclerView.setHasFixedSize(true);
GridLayoutManager layoutManager = new GridLayoutManager(AreaListActivity.this,3);
DividerItemDecoration dividerItemDecoration = new DividerItemDecoration(AreaListActivity.this, layoutManager.getOrientation());
recyclerView.addItemDecoration(dividerItemDecoration);
recyclerView.setLayoutManager(layoutManager);
//recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
// recyclerView.setLayoutManager(new GridLayoutManager(this,3));
// recyclerView.getLayoutManager().scrollToPosition(5);
}
public void populateRecyclerView(){
//pass the allAreaList to it
RecyclerViewAdapter adapter=new RecyclerViewAdapter(AreaListActivity.this,ArrayList,this);
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
//recyclerView.setAdapter(new RecyclerViewAdapter(MainActivity.this,this));
}
@Override
public void recyclerViewListClicked(View v, int position) {
Intent myIntent=new Intent(MainActivity.this,SecondActivity.class);
myIntent.putExtra("position",position);
startActivity(myIntent);
finish();
}
@Override
public void onBackPressed() {
Intent myIntent=new Intent(MainActivity.this,HomeActivity.class);
startActivity(myIntent);
finish();
}
}
RecyclerViewHolder Class
公共类RecyclerViewHolder扩展了RecyclerView.ViewHolder {
//view holder is for girdview as we used in the listView
public ImageView imageView,imageView2;
public RecyclerViewHolder(View itemView) {
super(itemView);
this.imageView=(ImageView)itemView.findViewById(R.id.image);
}
}
适配器类 公共类RecyclerView_Adapter扩展了RecyclerView.Adapter {
//RecyclerView will extend to recayclerview Adapter
private ArrayList<ModelClass> arrayList;
private Context context;
private static RecyclerViewClickListener itemListener;
//constructor of the RecyclerView Adapter
RecyclerView_Adapter(Context context,ArrayList<ModelClass> arrayList,RecyclerViewClickListener itemListener){
this.context=context;
this.arrayList=arrayList;
this.itemListener=itemListener;
}
@Override
public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//this method will inflate the custom layout and return as viewHolder
LayoutInflater layoutInflater=LayoutInflater.from(parent.getContext());
ViewGroup mainGroup=(ViewGroup) layoutInflater.inflate(R.layout.single_item,parent,false);
RecyclerViewHolder listHolder=new RecyclerViewHolder(mainGroup);
return listHolder;
}
@Override
public void onBindViewHolder(RecyclerViewHolder holder, final int position) {
final ModelClass modelClass=arrayList.get(position);
//holder
RecyclerViewHolder mainHolder=(RecyclerViewHolder)holder;
//convert the drawable image into bitmap
Bitmap image= BitmapFactory.decodeResource(context.getResources(),modelClass.getImage());
//set the image into imageView
mainHolder.imageView.setImageBitmap(image);
//to handle on click event when clicked on the recyclerview item and
// get it through the RecyclerViewHolder class we have defined the views there
mainHolder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//get the position of the image which is clicked
itemListener.recyclerViewListClicked(v,position);
}
});
}
@Override
public int getItemCount() {
return (null!=arrayList?arrayList.size():0);
}
}
接口
public interface RecyclerViewClickListener {
//this is method to handle the event when clicked on the image in Recyclerview
public void recyclerViewListClicked(View v,int position);
}
//并在活动中调用此方法 RecyclerView_Adapter adapter = new RecyclerView_Adapter(Wallpaper.this,arrayList,this); recyclerView.setAdapter(适配器); adapter.notifyDataSetChanged();
@Override
public void recyclerViewListClicked(View v,int position){
imageView.setImageResource(wallpaperImages[position]);
}