目前是 android 开发的初学者,我一直在这里和那里进行一些练习/教程,我遇到了一个问题,即 RecyclerView 中的项目之间的距离彼此相距太远,如下图所示.我如何使它们彼此更接近?我一直在搜索,但没有任何效果。
编辑:将 android:layout_height = "match_parent" 更改为 android:layout_height = "wrap_content" 后仍然没有变化布局。
所以我包含了我的java类
这是我的.xml
custom_row_news_items.xml
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/date_text"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true" />
<ImageView
android:src="@drawable/img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/thumb_img"
android:layout_below="@+id/date_text"
android:layout_centerHorizontal="true" />
<TextView
android:background="#006699"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/title_text"
android:layout_alignBottom="@+id/thumb_img"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Medium Text"
android:id="@+id/des_text"
android:layout_below="@+id/thumb_img"
android:layout_centerHorizontal="true" />
</RelativeLayout>
content_navigation_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_navigation_drawer"
tools:context="com.example.azrie.dummyvoicethenews.NavigationDrawer">
<android.support.v7.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recyclerView"/>
</RelativeLayout>
这是我的 java 类
NavigationDrawer.java
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class NavigationDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
RSSread rssRead;
rssRead = new RSSread(this,recyclerView);
rssRead.execute();
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation_drawer, 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);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
RSSread.java
public class RSSread extends AsyncTask{
Context context;
ProgressDialog progressDialog;
//RSS address
String address = "https://www.sciencemag.org/rss/news_current.xml";
URL url;
//Global initialization for other class to access
ArrayList<FeedItem> feedItems;
RecyclerView recyclerView;
public RSSread(Context context, RecyclerView recyclerView){
//Initialize recycle view
this.recyclerView = recyclerView;
//Initialize progress dialog
this.context = context;
progressDialog = new ProgressDialog(context);
progressDialog.setMessage("Loading....");
}
@Override
protected void onPreExecute() {
//display progress dialog before fetching data
progressDialog.show();
super.onPreExecute();
}
@Override
protected void onPostExecute(Object o) {
super.onPostExecute(o);
//dismiss the "Loading..." progress dialog
progressDialog.dismiss();
MyAdapter adapter = new MyAdapter(context,feedItems);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(adapter);
}
@Override
protected Object doInBackground(Object[] objects) {
ProcessXml(Getdata());
return null;
}
private void ProcessXml(Document data) {
//If data is present or not = null
if(data!=null){
//ArrayList is created to store every item into a single item
feedItems = new ArrayList<>();
//Return document element name that is RSS ( Exercise Part 1 )
//Log.d("Root", data.getDocumentElement().getNodeName());
//Element object that store "Root" element
Element root = data.getDocumentElement();
//Items are inside channel and channel tag is first child of root tag
Node channel = root.getChildNodes().item(1);
//Store all child of channel element
NodeList items = channel.getChildNodes();
//Loop through each child of element
//Output testing
//Log.d("ItemsLength",Integer.toString(items.getLength()));
for (int i = 0; i < items.getLength(); i++){
//Create new node that will store data
Node currentChild = items.item(i);
//Check currentChild node is item node
if (currentChild.getNodeName().equalsIgnoreCase("item")){
//Create a new feed item for every item
FeedItem item = new FeedItem(); //Exercise Part 2 : How to process data
NodeList itemChild = currentChild.getChildNodes();
//Loop through all childs with item tag
for(int j = 0; j < itemChild.getLength(); j++){
Node current = itemChild.item(j);
//Display context of Node Current ( Exercise Part 1 : How to process data )
//Log.d("textContent",current.getTextContent());
//check for node title node
if(current.getNodeName().equalsIgnoreCase("title")){
//Set title from FeedItem into current title
item.setTitle(current.getTextContent());
//Output test
Log.d("CurrentItemTitle",current.getTextContent());
}
else if(current.getNodeName().equalsIgnoreCase("description")){
//Set description from FeedItem into current description
item.setDescr(current.getTextContent());
//Output test
Log.d("CurrentItemDesp",current.getTextContent());
}
else if(current.getNodeName().equalsIgnoreCase("pubDate")){
//Set pubDate from FeedItem into current pubDate
item.setPubDate(current.getTextContent());
//Output test
Log.d("CurrentItemPubDate",current.getTextContent());
}
else if(current.getNodeName().equalsIgnoreCase("link")){
//Set link from FeedItem into current link
item.setLink(current.getTextContent());
//Output test
Log.d("CurrentItemLink",current.getTextContent());
}
else if(current.getNodeName().equalsIgnoreCase("media:thumbnail")){
String url = current.getAttributes().item(0).getTextContent();
item.setThumbnailUrl(url);
//Output test
Log.d("CurrentItemThumbnailUrl",current.getTextContent());
}
}
feedItems.add(item);
Log.d("ItemTitle",item.getTitle());
Log.d("ItemDescription",item.getDescr());
Log.d("ItemLink",item.getLink());
Log.d("ItemPubDate",item.getPubDate());
Log.d("ItemThumbnailUrl",item.getThumbnailUrl());
}
}
}
}
//method that return Document type
public Document Getdata(){
try {
//Passing the string to URL (From Address)
url = new URL(address);
//Open connection
HttpURLConnection connection;
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
InputStream inputStream = connection.getInputStream();
//Create new instance of document build
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
//Return XML document
Document xmlDoc = builder.parse(inputStream);
//Return xmlDoc
return xmlDoc;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
MyAdapter.java
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
ArrayList<FeedItem> feedItems;
Context context;
public MyAdapter(Context context,ArrayList<FeedItem> feedItems){
this.feedItems = feedItems;
this.context = context;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.custom_row_news_items,parent,false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
}
@Override
public int getItemCount() {
return feedItems.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View itemView) {
super(itemView);
}
}
}
抱歉有点乱。我还是不习惯这里的格式。
最佳答案
在custom_row_news_items.xml中,android:layout_height="match_parent"应该是android:layout_height="wrap_content。因为match_parent,它占用了整个屏幕。始终记住,如果 recyclerview 具有垂直布局,则单行的高度应为 wrap_content。
也可以尝试将recyclerview的高宽参数改成
android:layout_width="match_parent"
android:layout_height="match_parent"
我推测,因为您的高度设置为包裹内容,所以它只占一行。但是有了 match_parent,它将在屏幕上容纳尽可能多的内容。
关于android - RecyclerView 之间的距离太远,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38444092/
我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我正在检查一个Rails项目。在ERubyHTML模板页面上,我看到了这样几行:我不明白为什么不这样写:在这种情况下,||=和ifnil?有什么区别? 最佳答案 在这种特殊情况下没有区别,但可能是出于习惯。每当我看到nil?被使用时,它几乎总是使用不当。在Ruby中,很少有东西在逻辑上是假的,只有文字false和nil是。这意味着像if(!x.nil?)这样的代码几乎总是更好地表示为if(x)除非期望x可能是文字false。我会将其切换为||=false,因为它具有相同的结果,但这在很大程度上取决于偏好。唯一的缺点是赋值会在每次运行
📢博客主页:https://blog.csdn.net/weixin_43197380📢欢迎点赞👍收藏⭐留言📝如有错误敬请指正!📢本文由Loewen丶原创,首发于CSDN,转载注明出处🙉📢现在的付出,都会是一种沉淀,只为让你成为更好的人✨文章预览:一.分辨率(Resolution)1、工业相机的分辨率是如何定义的?2、工业相机的分辨率是如何选择的?二.精度(Accuracy)1、像素精度(PixelAccuracy)2、定位精度和重复定位精度(RepeatPrecision)三.公差(Tolerance)四.课后作业(Post-ClassExercises)视觉行业的初学者,甚至是做了1~2年
最近因为项目需要,需要将Android手机系统自带的某个系统软件反编译并更改里面某个资源,并重新打包,签名生成新的自定义的apk,下面我来介绍一下我的实现过程。APK修改,分为以下几步:反编译解包,修改,重打包,修改签名等步骤。安卓apk修改准备工作1.系统配置好JavaJDK环境变量2.需要root权限的手机(针对系统自带apk,其他软件免root)3.Auto-Sign签名工具4.apktool工具安卓apk修改开始反编译本文拿Android系统里面的Settings.apk做demo,具体如何将apk获取出来在此就不过多介绍了,直接进入主题:按键win+R输入cmd,打开命令窗口,并将路
由于匿名block和散列block看起来大致相同。我正在玩它。我做了一些严肃的观察,如下所示:{}.class#=>Hash好的,这很酷。空block被视为Hash。print{}.class#=>NilClassputs{}.class#=>NilClass为什么上面的代码和NilClass一样,下面的代码又显示了Hash?puts({}.class)#Hash#=>nilprint({}.class)#Hash=>nil谁能帮我理解上面发生了什么?我完全不同意@Lindydancer的观点你如何解释下面几行:print{}.class#NilClassprint[].class#A
在许多ruby类之间共享记录器实例的最佳(正确)方法是什么?现在我只是将记录器创建为全局$logger=Logger.new变量,但我觉得有更好的方法可以在不使用全局变量的情况下执行此操作。如果我有以下内容:moduleFooclassAclassBclassC...classZend在所有类之间共享记录器实例的最佳方式是什么?我是以某种方式在Foo模块中声明/创建记录器还是只是使用全局$logger没问题? 最佳答案 在模块中添加常量:moduleFooLogger=Logger.newclassAclassBclassC..
之前有人问过这个问题,我发现了以下clip关于如何一次设置一个类对象的所有属性,但由于批量分配保护,这在Rails中是不可能的。(例如,您不能Object.attributes={})有没有一种很好的方法可以将一个类的属性合并到另一个类中?object1.attributes=object2.attributes.inject({}){|h,(k,v)|h[k]=vifObjectModel.column_names.include?(k);h}谢谢。 最佳答案 利用assign_attributes使用:without_prote
我有三个模型:User、Product、Offer以及这些模型之间的关系问题。场景:用户1发布了一个产品用户2可以向用户1发送报价,例如10美元用户1可以接受或拒绝提议我现在的问题是:用户、产品和报价之间的正确关系是什么?我如何处理那些“接受或拒绝”操作?是否有更好的解决方案?用户模型:classUser:productsend产品型号:classProduct:usersend提供模型:classOffer提前致谢:)编辑:我正在使用Rails3.2.8 最佳答案 警告:小小说来了第1部分:设置关联我建议阅读Railsguideo
标题本身就说明了......read_timeout和open_timeout之间有什么区别? 最佳答案 open_timeout是您愿意等待“打开连接”的时间。在TCP上下文中,在放弃尝试并引发超时错误之前等待握手完成的时间量。read_timeout您可能会猜到,是您愿意等待从连接方接收到某些数据的时间。一个例子可能会清楚地说明这一点:在SOAPoverHTTPoverTCP上下文中(简化):您尝试与服务器建立TCP连接。如果建立连接的时间比open_timeout长,则放弃连接尝试并引发/发出/返回超时错误。如果连接成功,您发