Android Recycler布局没有正确膨胀原因

时间:2016-01-27 15:49:00

标签: android layout android-recyclerview recycler-adapter

我使用android recyclerview来扩充三个布局,如我的代码所示,但页脚没有正确充气,因此隐藏了listview布局的一些数据。我的代码存在问题,因为我希望listview布局显示为recyclerview适配器中的最后一个布局。

package com.news254.inception.newsapp;

 import android.content.Intent;
import android.content.res.Configuration;
 import android.support.v4.widget.DrawerLayout;
 import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.support.v7.widget.LinearLayoutManager;
 import android.support.v7.widget.RecyclerView;
 import android.support.v7.widget.Toolbar;
 import android.view.Menu;
 import android.view.MenuItem;
 import android.view.View;
  import android.widget.ImageView;

    public class Newspaper extends AppCompatActivity {
 Toolbar mtoolbar;
  String TITLES[] = {"Newspaper","Gossip","Entertainment","Sports","International","About","Exit"};

int ICONS[] = {R.drawable.newspaper,R.drawable.gossip,R.drawable.entertainment,R.drawable.sports,R.drawable.international,R.drawable.about,R.drawable.exit};

//Similarly we Create a String Resource for the name and email in the header     
  view
//And we also create a int resource for profile picture in the header view
 Boolean isDrawerOpen =true;


private Toolbar toolbar;                              // Declaring the               

Toolbar Object

 RecyclerView mRecyclerView;                           // Declaring      
RecyclerView
RecyclerView.Adapter mAdapter;                        // Declaring Adapter     
For Recycler View
RecyclerView.LayoutManager mLayoutManager;            // Declaring Layout    
 Manager as a linear layout manager
 DrawerLayout Drawer;                                  // Declaring     
 DrawerLayout

  ActionBarDrawerToggle mDrawerToggle;
  ImageView       
Nation,Standard,Capital,Nairobian,BusinessDaily,EastAfrican,Star,Mwakilishi,Citizen;
 String nation  = "http://www.nation.co.ke";
String standard = "http://www.standardmedia.co.ke";
String star = "http://www.the-star.co.ke";
String  nairobian = "http://www.sde.co.ke/m/thenairobian";
 String eastafrican = "http://www.theeastafrican.co.ke";
String businessdaily = "http://www.businessdailyafrica.com";
String mwakilishi = "http://www.mwakilishi.com";
String capital  = "http://www.capitalfm.co.ke";
String citizen = "http://www.theweeklycitizen.co.ke";

@Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

setContentView(R.layout.activity_news);


mtoolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mtoolbar);


mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View

mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size

mAdapter = new MyAdapter(TITLES,ICONS);       // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
// And passing the titles,icons,header view name, header view email,
// and header view profile picture

mRecyclerView.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView

mLayoutManager = new LinearLayoutManager(this);                 // Creating a layout Manager

mRecyclerView.setLayoutManager(mLayoutManager);                 // Setting the layout Manager
Nation = (ImageView) findViewById(R.id.nation);
Nation.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",nation);
        startActivity(p);

    }
});
Standard = (ImageView) findViewById(R.id.standard);
Standard.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",standard);
        startActivity(p);

    }
});
     Star = (ImageView) findViewById(R.id.star);
      Star.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Entertainment.class);
        p.putExtra("site",star);
        startActivity(p);

    }
});
Capital = (ImageView) findViewById(R.id.capital);
Capital.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",capital);
        startActivity(p);

    }
});
Nairobian = (ImageView) findViewById(R.id.nairobian);
Nairobian.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",nairobian);
        startActivity(p);

    }
});
Citizen = (ImageView) findViewById(R.id.citizen);
Citizen.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",citizen);
        startActivity(p);

    }
});
EastAfrican = (ImageView) findViewById(R.id.eastafrican);
EastAfrican.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",eastafrican);
        startActivity(p);

    }
});
BusinessDaily = (ImageView) findViewById(R.id.businessdaily);
BusinessDaily.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",businessdaily);
        startActivity(p);

    }
});
Mwakilishi = (ImageView) findViewById(R.id.mwakilishi);
Mwakilishi.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent p = new Intent(Newspaper.this,Browser.class);
        p.putExtra("site",mwakilishi);
        startActivity(p);

    }
});

Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);        // Drawer object Assigned to the view
mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.drawer_open,R.string.drawer_close){

    @Override
    public void onDrawerOpened(View drawerView) {
        super.onDrawerOpened(drawerView);
        // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
        // open I am not going to put anything here)
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        super.onDrawerClosed(drawerView);
        // Code here will execute once drawer is closed
    }

  /*  @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {

        if(slideOffset >.55 && !isDrawerOpen)
        {
            onDrawerOpened(drawerView);
            isDrawerOpen=true;
        }
        else if (slideOffset <.45 && isDrawerOpen)
        {
            onDrawerClosed(drawerView);
            isDrawerOpen = false;
        }
       // super.onDrawerSlide(drawerView, slideOffset);
    }*/
}; // Drawer Toggle Object Made
Drawer.setDrawerListener(mDrawerToggle);

// / Drawer Listener set to the Drawer toggle


getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mDrawerToggle.setDrawerIndicatorEnabled(true);

}

   @Override
     public void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}

 @Override
   public void onConfigurationChanged(Configuration newConfig) {
     super.onConfigurationChanged(newConfig);
      mDrawerToggle.onConfigurationChanged(newConfig);
      }

   @Override
    public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_news, 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();
 if(mDrawerToggle.onOptionsItemSelected(item))
{
    return true;
}
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
    return true;
}

return super.onOptionsItemSelected(item);
 } }

  package com.news254.inception.newsapp;

   import android.support.v7.widget.RecyclerView;

    import android.support.v7.widget.RecyclerView;
   import android.view.LayoutInflater;
   import android.view.View;
   import android.view.ViewGroup;
   import android.widget.ImageView;
   import android.widget.TextView;
    import android.widget.Toast;

   /**
     * Created by hp1 on 28-12-2014.
      */
 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

private static final int TYPE_FOOTER = 2;  // Declaring Variable to     
 Understand which View is being worked on
 // IF the view under inflation and population is header or Item
  private static final int TYPE_ITEM = 1;
   private static final int TYPE_HEADER= 0;

  private String mNavTitles[]; // String Array to store the passed titles 
   Value from MainActivity.java
       private int mIcons[];       // Int Array to store the passed icons       
          resource value from MainActivity.java
   private int mfooter[];
  private String mtitleFoot[];
  int Holderid;

      // Creating a ViewHolder which extends the RecyclerView View Holder
       // ViewHolder are used to to store the inflated views in order to   
            recycle them

   public static class ViewHolder extends RecyclerView.ViewHolder {
int Holderid;

TextView textView;
ImageView imageView;
ImageView about;
TextView name;



public ViewHolder(View itemView,int ViewType) {                 // Creating ViewHolder Constructor with View and viewType As a parameter
    super(itemView);


    // Here we set the appropriate view in accordance with the the view type as passed when the holder object is created

    if(ViewType == TYPE_ITEM) {
        textView = (TextView) itemView.findViewById(R.id.rowText); // Creating TextView object with the id of textView from item_row.xml
        imageView = (ImageView) itemView.findViewById(R.id.rowIcon);// Creating ImageView object with the id of ImageView from item_row.xml
        Holderid = 1;                                               // setting holder id as 1 as the object being populated are of type item row
    }
   else if(ViewType == TYPE_FOOTER){


        about = (ImageView) itemView.findViewById(R.id.image);         // Creating Text View object from header.xml for name
        name = (TextView) itemView.findViewById(R.id.text);       // Creating Text View object from header.xml for email
       // Creating Image view object from header.xml for profile pic
        Holderid =2;                                                // Setting holder id = 0 as the object being populated are of type header view
    }
    else if(ViewType == TYPE_HEADER)
    {
        Holderid =0;

      }
  }


  }



           MyAdapter(String Titles[],int Icons[]){ // MyAdapter Constructor 
            with titles and icons parameter
               // titles, icons, name, email, profile pic are passed from the   
                 main activity as we
               mNavTitles = Titles;                //have seen earlier
               mIcons = Icons;
                //here we assign those passed values to the values 
                   we declared here
                   //in adapter



    }



//Below first we ovverride the method onCreateViewHolder which is called when   
 the ViewHolder is
   //Created, In this method we inflate the item_row.xml layout if the 
      viewType is Type_ITEM or else we inflate header.xml
   // if the viewType is TYPE_HEADER
    // and pass it to the view holder

    @Override
        public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int    
               viewType) {

if (viewType == TYPE_ITEM) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.listview,parent,false); //Inflating the layout

    ViewHolder vhItem = new ViewHolder(v,viewType); //Creating ViewHolder and passing the object of type view

    return vhItem; // Returning the created object

    //inflate your layout and pass it to view holder

}
else if (viewType == TYPE_HEADER) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.header, parent, false); //Inflating the layout

    ViewHolder vhHeader = new ViewHolder(v, viewType); //Creating ViewHolder and passing the object of type view

    return vhHeader; //returning the object created
}

else if  (viewType == TYPE_FOOTER) {

    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer, parent, false); //Inflating the layout

    ViewHolder vhHeader = new ViewHolder(v, viewType); //Creating ViewHolder and passing the object of type view

    return vhHeader; //returning the object created
}
    return null;

        }

   //Next we override a method which is called when the item in a row is 
      needed to be displayed, here the int position
     // Tells us item at which position is being constructed to be displayed 
        and the holder id of the holder object tell us
       // which view type is being created 1 for item row
     @Override
         public void onBindViewHolder(MyAdapter.ViewHolder holder, int 
         position) {
if(holder.Holderid ==1) {                              // as the list view is going to be called after the header view so we decrement the
    // position by 1 and pass it to the holder while setting the text and image
    holder.textView.setText(mNavTitles[position-2]); // Setting the Text with the array of our Titles
    holder.imageView.setImageResource(mIcons[position-2]);// Settimg the image with array of our icons
}
else if (holder.Holderid ==2){





     holder.about.setImageResource(mIcons[position]);          // Similarly we set the resources for header view
        holder.name.setText(mNavTitles[position]);



}
else if (holder.Holderid ==0){



}
   }

   // This method returns the number of items present in the list
         @Override
         public int getItemCount() {

return mNavTitles.length+2;

// the number of items in the list will be +1 the titles including the header view.
}


 // Witht the following method we check what type of view is being passed
 @Override
   public int getItemViewType(int position) {


if (position == 0) {
    return TYPE_HEADER;
}
 else if (position ==2) {
    return TYPE_FOOTER ;
}
else  {
    return TYPE_ITEM;
     }
 }

  private boolean isPositionHeader(int position) {
  return position == 0;
}


   }

1 个答案:

答案 0 :(得分:0)

更改此

   @Override
   public int getItemViewType(int position) {
     if (position == 0) {
       return TYPE_HEADER;
      }
     else if (position ==2) {
      return TYPE_FOOTER ;
     }
     else  {
     return TYPE_ITEM;
     }
   }  

   @Override
    public int getItemViewType(int position) {
    if(position==0){
        return TYPE_HEADER;
    }

    else if((mNavTitles.length+ 1) == position){
        return TYPE_FOOTER;
    }else{
         return TYPE_ITEM;
    }
}