List Item View In A Relativelayout
I am developing a sample application . I want to list data show in RelativeLayout. But it is not show accurately. I want to view : But it is view : My xml code:
Solution 1:
You should inflate layout in your getView, not content.
ViewrowView= inflater.inflate(R.id.content, parent, false);
should be
ViewrowView= inflater.inflate(R.layout.layout_name, parent, false);
EDIT: You shouldn't put your header in every list item. In your list item just put text view and put header in your main layout.
Main layout:
<RelativeLayout....
<!--Headerlayout--><ListView /></RelativeLayout>
List item layout:
<RelativeLayout...
<TextView /></RelativeLayout>
After that just put in inflater layout.list_item
Solution 2:
Try this way,hope this will help you to solve your problem.
activity_main.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:id="@+id/topeFixContent"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="#FC9000"android:gravity="center_vertical" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="#DCF4FC"><Buttonandroid:id="@+id/menuButton"android:layout_width="46dp"android:layout_height="32dp"android:background="@drawable/menu" /><Viewandroid:layout_width="0dp"android:layout_height="1dp"android:layout_weight="1"/><ImageViewandroid:id="@+id/imageView1"android:layout_width="46dp"android:layout_height="30dp"android:layout_gravity="top|right"android:contentDescription="@string/imgLogo"android:adjustViewBounds="true"android:src="@drawable/logo" /></LinearLayout></LinearLayout><LinearLayoutandroid:id="@+id/header"android:layout_width="match_parent"android:layout_height="30dp"android:background="#FA9000"android:gravity="center"><TextViewandroid:id="@+id/hedderText"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Title"/></LinearLayout><ListViewandroid:id="@+id/listNews"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"/></LinearLayout>
list_item.xml
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="center_vertical" ><TextViewandroid:id="@+id/bdNewsTex"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="18sp"/></LinearLayout>
MainActivity.java
publicclassMainActivityextendsActivity{
private ListView listNews;
private NewsAdapter newsAdapter;
private ArrayList<String> newsList;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listNews = (ListView) findViewById(R.id.listNews);
newsList = new ArrayList<String>();
newsList.add("News 1");
newsList.add("News 2");
newsList.add("News 3");
newsList.add("News 4");
newsList.add("News 5");
newsList.add("News 6");
newsList.add("News 7");
newsList.add("News 8");
newsList.add("News 9");
newsAdapter = new NewsAdapter(this,newsList);
listNews.setAdapter(newsAdapter);
listNews.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
publicvoidonItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this,newsList.get(position),Toast.LENGTH_SHORT).show();
}
});
}
classNewsAdapterextendsBaseAdapter{
private Context context;
private ArrayList<String> newsList;
publicNewsAdapter(Context context,ArrayList<String> newsList){
this.context = context;
this.newsList = newsList;
}
@Override
publicintgetCount() {
return newsList.size();
}
@Override
public Object getItem(int position) {
return newsList.get(position);
}
@Override
publiclonggetItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.list_item,null,false);
holder.bdNewsTex = (TextView) convertView.findViewById(R.id.bdNewsTex);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.bdNewsTex.setText(newsList.get(position));
return convertView;
}
classViewHolder {
TextView bdNewsTex;
}
}
}
Post a Comment for "List Item View In A Relativelayout"