2016-07-18 48 views
5

目前在Android開發的初學者,我一直在一些練習/教程在這裏和那裏,我面臨一個問題,其中RecyclerView中的項目之間的距離相距太遠如下圖所示。我如何使它更接近彼此?我一直在搜索,但沒有任何工作。RecyclerView之間的距離太遠

編輯:在將android:layout_height = "match_parent"更改爲android:layout_height = "wrap_content"之後,仍然沒有更改佈局。

所以,我包括我的java類一起

RecyclerView Screenshot

這是我.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); 
    } 
} 
} 

抱歉它是一個有點凌亂。我仍然不習慣這裏的格式。

回答

3

在custom_row_news_items.xml中,android:layout_height="match_parent"應該是android:layout_height="wrap_content。由於match_parent,它佔用了整個屏幕的空間。請務必記住,如果回收站視圖具有垂直佈局,則該行應具有高度wrap_content

您也可以嘗試改變recyclerview高度寬度參數

android:layout_width="match_parent" 
    android:layout_height="match_parent" 

我猜測,因爲你的高度設置爲包裹的內容,它只是採取單行。但是通過match_parent,它可以容納儘可能多的屏幕。

+0

你好,非常感謝你的回覆。現在感謝您的解釋,我更瞭解它是如何工作的。但即使更改了'android:layout_height',它們之間仍然存在很大差距。也許我的Java有什麼問題呢? –

+0

也請理解如何在relativelayouts中使用align參數。你已經說過'title_text' alignBottom和'thumb_img',但都有'layout_width =「match_parent」'。所以這意味着textview和imageview都在同一行並佔據整個屏幕寬度。這是不可能的。但它沒有給出任何錯誤,仍然這樣做是錯誤的 – suku

+0

謝謝,我應該學習更多的語法。現在解決了。祝你有個美好的一天:) –

1

修改您的custom_row_news_items.xml如下。

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <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> 

設置父RelativeLayout高度wrap_content。目前其設置爲match_parent這是佔用的空間。

+0

你好,謝謝。我試過你的解決方案,但問題依然存在。我把我的java代碼放在一起,認爲問題的根源可能源於此。 –

2

剛剛被android:layout_height="match_parent"android:layout_height="wrap_content改變你的父佈局的高度,這將解決您的問題,爲您排佈置的容器已WRAP_CONTENT和你的行佈局嘗試匹配父的高度將填補全屏幕,和你的單組件已經佔據了整個屏幕的高度。

改變行佈局:

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"> 

    <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> 
+0

你好,感謝你的幫助。我已經嘗試過,但似乎沒有任何改變。它之間還有很大的差距。我發佈我的java類,認爲問題可能源於那裏。 –

1

當你改變RelativeLayout的高度wrap_content因爲RelativeLayout不知道本身應該有多高是這是發生均勻。

RelativeLayout的高度設置爲wrap_content,並嘗試將以下內容添加到最後的TextView

android:layout_alignParentBottom="true" 

這告訴RelativeLayout的底部與最後TextView對齊,所以現在RelativeLayout知道到哪裏結束。