我目前在为客户工作的应用程序上有一个页面。有问题的页面顶部有一个图像,这是我一直在使用的横幅。下面是一个回收者 View ,其中包含包含每天不断变化的新闻信息的卡片。我之前在这个应用程序的 iOS 版本上这样做过,我将图像横幅放入最顶部的 recyclerview(在 iOS 上称为集合)中,然后当您滚动 recyclerview 时,图像会上下移动卷轴。有什么方法可以将图像横幅设置到最顶部的回收器 View 中,以便它与回收器 View 中的卡片一起上下滚动,而不是在回收器 View 之外。
图像横幅占用了大量空间,这就是为什么我想将其插入回收器 View ,以便在滚动以同时查看更多卡片时释放屏幕上的空间。见下图
How the recyclerview looks without scrolling How the recyclerview looks after scrolling
public class News extends Fragment {
private static final String url = "somenewsurl"; //news url
private LinearLayoutManager linearLayoutManager;
private List<NewsItem> news;
private RecyclerView rv;
private RecyclerView.Adapter adapter;
public News() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_news, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
//initialize variables
news = new ArrayList<>();
adapter = new NewsRvAdapter(news, getActivity().getApplicationContext());
//set up linear layout manager
linearLayoutManager = new LinearLayoutManager(getActivity());
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
//set up recycler view
rv = getView().findViewById(R.id.news_rv);
rv.setHasFixedSize(true);
rv.setLayoutManager(linearLayoutManager);
rv.setAdapter(adapter);
newsData(); //parse json data
}
这是此页面使用的另一个文件,其中包含图像和 recylerview
public class NewsRvAdapter extends RecyclerView.Adapter<NewsRvAdapter.MyViewHolder> {
private Context context;
private List<NewsItem> newsList;
//default constructor
public NewsRvAdapter(){}
public NewsRvAdapter(List<NewsItem> newsList, Context context){
this.newsList = newsList;
this.context = context;
}
@NonNull
@Override
public NewsRvAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.news_card, viewGroup, false);
final MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull final NewsRvAdapter.MyViewHolder myViewHolder, int i) {
myViewHolder.title.setText(newsList.get(i).getTitle());
myViewHolder.description.setText(newsList.get(i).getDescription());
//Load image URL using Picasso
Picasso.get()
.load(newsList.get(i).getImageUrl())
.centerCrop()
.fit()
.into(myViewHolder.photoUrl);
//on click listener that opens url in webview
myViewHolder.photoUrl.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
String link = newsList.get(myViewHolder.getAdapterPosition()).getLink();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse(link));
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}
});
//onclick that opens new activity
myViewHolder.description.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent i = new Intent(context, SingleNews.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.putExtra("title", newsList.get(myViewHolder.getAdapterPosition()).getTitle());
i.putExtra("urlToImage", newsList.get(myViewHolder.getAdapterPosition()).getImageUrl());
i.putExtra("publishedAt", newsList.get(myViewHolder.getAdapterPosition()).getDate());
i.putExtra("content", newsList.get(myViewHolder.getAdapterPosition()).getContent());
context.startActivity(i);
}
});
}
@Override
public int getItemCount() {
return newsList.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView title, description;
ImageView photoUrl;
LinearLayout viewContainer;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
title = itemView.findViewById(R.id.news_title);
description = itemView.findViewById(R.id.news_description);
photoUrl = itemView.findViewById(R.id.news_photo);
viewContainer = itemView.findViewById(R.id.newsView_container);
}
}
}
这是 xml 文件
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@drawable/gradient"
tools:context=".News">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/news" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/img"
android:id="@+id/news_rv">
</android.support.v7.widget.RecyclerView>
错误如下: 2019-08-19 11:36:13.788 14829-14829/com.example.gabe.politicianspulse E/AndroidRuntime: 致命异常: main 进程:com.example.gabe.politicianspulse,PID:14829 android.view.InflateException:com.example.gabe.politicianspulse 中的二进制 XML 文件行 #10:layout/fragment_news:com.example.gabe.politicianspulse 中的二进制 XML 文件行 #10:layout/fragment_news:膨胀类 androidx.core 时出错.widget.NestedScrollView 引起原因: android.view.InflateException:com.example.gabe.politicianspulse 中的二进制 XML 文件第 10 行:layout/fragment_news:膨胀类 androidx.core.widget.NestedScrollView 时出错 引起原因:java.lang.ClassNotFoundException:androidx.core.widget.NestedScrollView 在 java.lang.Class.classForName( native 方法)
最佳答案
您可以使用 NestedScrollView 包装您的布局
会是这样的
<?xml version="1.0" encoding="utf-8"?>
<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"
android:background="@drawable/gradient"
tools:context=".News">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="100dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/news" />
<android.support.v7.widget.RecyclerView
android:id="@+id/news_rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/img" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</RelativeLayout>
并且不要忘记在 recyclerview
上禁用嵌套滚动
通过使用您的 recyclerView
引用对象调用 setNestedScrollingEnabled(false);
。
关于java - 如何在RecyclerView之上插入图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57561757/