2016-07-18 6 views
5

वर्तमान में एंड्रॉइड विकास में एक शुरुआतकर्ता है और मैं यहां और वहां कुछ अभ्यास/ट्यूटोरियल का पालन कर रहा हूं और मुझे एक समस्या का सामना करना पड़ रहा है जहां रीसाइक्लिंग व्यू में वस्तुओं के बीच की दूरी एक-दूसरे से अलग है नीचे दी गई तस्वीर में दिखाया गया है। मैं इसे एक-दूसरे के करीब कैसे बना सकता हूं? मैं पूरी तरह से खोज रहा हूं लेकिन कुछ भी काम नहीं करता है।रीसाइक्लर व्यू के बीच की दूरी बहुत दूर

संपादित करें:android:layout_height = "wrap_content" को android:layout_height = "match_parent" बदलने के बाद वहाँ अभी भी लेआउट में कोई परिवर्तन नहीं है।

तो मैं साथ

RecyclerView Screenshot

यहाँ मेरी 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; 
    } 

} 

} 
है 210 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 होना चाहिए यदि इसमें लंबवत लेआउट है।

तुम भी

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

को recyclerview ऊंचाई चौड़ाई पैरामीटर बदल रहा अटकलें रहा हूँ कि क्योंकि अपनी ऊंचाई सामग्री रैप करने के लिए सेट कर दिया जाता है, यह केवल एक ही पंक्ति ले जा रहा है की कोशिश कर सकते हैं। लेकिन match_parent के साथ, यह स्क्रीन पर जितना हो सके उतना ही समायोजित करेगा।

+0

हैलो और आपके उत्तर के लिए धन्यवाद। अब आपके स्पष्टीकरण के लिए धन्यवाद, मुझे और समझ है कि यह कैसे काम करता है। लेकिन 'एंड्रॉइड बदलने के बाद भी: लेआउट_हेइट' अभी भी इसमें एक बड़ा अंतर है। शायद मेरे जावा के साथ कुछ गड़बड़ है? –

+0

कृपया यह भी समझें कि relativelayouts में संरेखण पैरामीटर का उपयोग कैसे करें। आपने 'title_text' alignBottom' thumb_img' के साथ कहा है लेकिन दोनों में 'layout_width =" match_parent "' है। तो इसका मतलब है कि टेक्स्टव्यू और छविदृश्य दोनों एक ही पंक्ति में हैं और कुल स्क्रीन चौड़ाई पर कब्जा करते हैं। यह संभव नहीं हो सकता है। लेकिन यह कोई त्रुटि नहीं दे रहा है, फिर भी ऐसा करना गलत है – 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> 

wrap_content के रूप में माता पिता RelativeLayout ऊंचाई निर्धारित करें। वर्तमान में यह match_parent पर सेट है जो रिक्त स्थान ले रहा है।

+0

हैलो और धन्यवाद। मैंने आपके समाधान की कोशिश की है लेकिन समस्या अभी भी बनी हुई है। मैंने अपने जावा कोड को एक साथ शामिल किया जिसमें समस्या का स्रोत वहां से उत्पन्न हो सकता है। –

2

सिर्फ android:layout_height="wrap_content को android:layout_height="match_parent" किया जा रहा से अपने माता पिता के लेआउट ऊंचाई बदल सकते हैं और के रूप में पंक्ति लेआउट के अपने कंटेनर 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

हैलो और आपकी मदद के लिए धन्यवाद। मैंने कोशिश की है लेकिन कुछ भी बदलना प्रतीत नहीं होता है। इसके बीच अभी भी बड़ा अंतर है। मैं अपनी जावा कक्षा पोस्ट करता हूं सोच रहा हूं कि समस्या वहां से उत्पन्न हो सकती है। –

1

यह भी हो रहा है जब आप RelativeLayout के लिए wrap_content क्योंकि RelativeLayout यह नहीं पता कि लंबा ही होना चाहिए ऊंचाई बदल जाते हैं।

RelativeLayout की ऊंचाई wrap_content पर सेट करें और अंतिम TextView पर निम्न को जोड़ने का प्रयास करें।

android:layout_alignParentBottom="true" 

यह RelativeLayout के नीचे बताता पिछले TextView साथ गठबंधन किया जाना है, इसलिए अब RelativeLayout जानता है, जहां समाप्त करने के लिए।

संबंधित मुद्दे