-1

I am trying to indent listView row by certain factor. For this, I used view.layout() and view.setX() method but they don't work or result in forceclose. Is there a way to achieve this result. Here is my code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View newRowView = convertView;
    ViewHolder viewHolder;



    if(newRowView == null)
    {   

    LayoutInflater inflator = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);


    newRowView = inflator.inflate(R.layout.list_view_images, parent,false); 

    //newRowView.layout(100*(position/list.size()), 0, 0, 0); // not working

    newRowView.setX(100*(position/list.size())); // causing force close

    viewHolder = new ViewHolder();


    viewHolder.appIcon = (ImageView) newRowView.findViewById(R.id.imageView1);

    viewHolder.appName = (TextView) newRowView.findViewById(R.id.textView1);

    viewHolder.appName.setText(names.get(position));


    newRowView.setTag(viewHolder);

}

list_view_images.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_launcher" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />

</LinearLayout>

I am trying to indent the list in semi circular fashion. What can I do achieve required results?

Regards

Naruto
  • 1,710
  • 7
  • 28
  • 39
  • 1
    Have you considered using `margin` value to give that effect? – Amulya Khare Dec 31 '13 at 06:01
  • I couldn't find margin related method in view class (or may be I overlooked it). For margin I tried setLeft() and setX() both result in Force close – Naruto Dec 31 '13 at 06:02
  • Can you post your item `XML` for R.layout.list_view_images. – Amulya Khare Dec 31 '13 at 06:09
  • Also can you post an example of what you want? If you do `100*(position/list.size())` what you are doing now, you will get `\\` (slanted) shaped list not semi-circular – Amulya Khare Dec 31 '13 at 06:26
  • 100*(position/list.size()) is just for testing purpose to check if the list rows are indented. – Naruto Dec 31 '13 at 06:48
  • u can try setting paddding as well – vipul mittal Dec 31 '13 at 06:57
  • also check if `list` is initialized somewhere. And if list has atleast one item. If not you are dividing by zero `100*(position/list.size())`. I see your XML. You can easly set the margins. – Amulya Khare Dec 31 '13 at 07:05
  • I made sure it has list has items and it is intialized. The list works fine if I don't try to indent with setX() or setLeft() – Naruto Dec 31 '13 at 07:11

1 Answers1

0

Update your getView() method as follows:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View newRowView = convertView;
    ViewHolder viewHolder;

    if (newRowView == null) {

        LayoutInflater inflator = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        newRowView = inflator.inflate(R.layout.list_view_images, parent, false);

        viewHolder = new ViewHolder();
        viewHolder.innerContainer = (LinearLayout) newRowView.findViewById(R.id.innerContainer);
        viewHolder.appIcon = (ImageView) newRowView.findViewById(R.id.imageView1);
        viewHolder.appName = (TextView) newRowView.findViewById(R.id.textView1);
        viewHolder.appName.setText(list.get(position));
        newRowView.setTag(viewHolder);
    }
    else {
        viewHolder = (ViewHolder) newRowView.getTag();
    }

    float shiftRight = 100 * ((position * 1.0f) / list.size());
    viewHolder.innerContainer.setPadding((int) shiftRight, 0, 0, 0);

    //...
    //...

    return newRowView;
}

Update your XML layout as follows:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/innerContainer"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="30dp"
            android:layout_height="30dp"
            android:src="@drawable/ic_launcher" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />

    </LinearLayout>

</LinearLayout>

Update your ViewHolder to contain another field as following:

public LinearLayout innerContainer;

Explanation: Basically, we put your original XML layout inside a LinearLayout that acts as an outer container. The we assign padding to the innerContainer (original layout) with respect to the outer layout

Amulya Khare
  • 7,718
  • 2
  • 23
  • 38
  • @Naruto I can give you code for semi circle if you need. Do let me know if the above works or not. – Amulya Khare Dec 31 '13 at 08:35
  • There is only one major mistake in my logic, I am indenting based on the list items (adapter position) which results in continous increase in indentation value, Is there a way to indent based on the view position on the screen, considering that only 5 views (rows) are displayed on the screen at a time. – Naruto Dec 31 '13 at 09:36
  • sure .That would be great. – Naruto Dec 31 '13 at 09:36
  • I can provide you semi-circle code. But to do it based on view position is quite tricky. It can be done, but that probably requires `scroll listener` and other logic. My suggestion is to post a new question with your updated code.. so maybe someone else could help you out. You can mark it answered if my answer helps you for the current bit. Cheers :) – Amulya Khare Dec 31 '13 at 16:54
  • I have already done it with scroll listener, I asked you, so if you had better code, I would have used it. I just want to know if you know how can I force the list view to always show n number of elements on screen, instead of showing partial elements when scrolled – Naruto Dec 31 '13 at 17:22