单击列表视图中的项目时,如何更改每行的布局

时间:2015-05-13 04:50:00

标签: android listview layout

我正在尝试创建一个包含21种不同布局的传记应用。在我的列表视图中,有21行。这些行中的每一行都具有不同的布局。我搜索了很多,但我找不到任何令人满意的问题答案。所有Java代码如下。提前谢谢!

public class MainActivity extends ActionBarActivity {

String[] names;
String[] descriptions;
int[] images = {R.drawable.scientist_1, R.drawable.scientist_2, R.drawable.scientist_3, R.drawable.scientist_4, R.drawable.scientist_5, R.drawable.scientist_6, R.drawable.scientist_7, R.drawable.scientist_8, R.drawable.scientist_9, R.drawable.scientist_10,R.drawable.scientist_11, R.drawable.scientist_12, R.drawable.scientist_13, R.drawable.scientist_14, R.drawable.scientist_15, R.drawable.scientist_16, R.drawable.scientist_17, R.drawable.scientist_18, R.drawable.scientist_19, R.drawable.scientist_20, R.drawable.scientist_21};

Toolbar toolbar;
ListView list;

private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from activity_main.xml
    setContentView(R.layout.activity_main);

    // Creating The Toolbar and setting it as the Toolbar for the activity
    toolbar = (Toolbar) findViewById(R.id.tool_bar);
    setSupportActionBar(toolbar);
    toolbar.setLogo(R.mipmap.ic_launcher);

    // Locate the ListView in activity_main.xml
    list = (ListView) findViewById(R.id.list);

    Resources res = getResources();
    names = res.getStringArray(R.array.names);
    descriptions = res.getStringArray(R.array.descriptions);

    MyAdapter adapter = new MyAdapter(this, names, images, descriptions);
    // Binds the Adapter to the ListView
    list.setAdapter(adapter);
}


class MyAdapter extends ArrayAdapter<String> {
    // Declare Variables
    Context context;
    int[] images;
    String[] nameArray;
    String[] descriptionArray;

    MyAdapter(Context c, String[] names, int imgs[], String[] desc) {
        super(c, R.layout.single_row, R.id.textView1, names);
        this.context = c;
        this.images = imgs;
        this.nameArray = names;
        this.descriptionArray = desc;

        Resources res = c.getResources();
        String[] nameArray = res.getStringArray(R.array.names);
        String[] descriptionArray = res.getStringArray(R.array.descriptions);
        int[] images = {R.drawable.scientist_1, R.drawable.scientist_2, R.drawable.scientist_3, R.drawable.scientist_4, R.drawable.scientist_5, R.drawable.scientist_6, R.drawable.scientist_7, R.drawable.scientist_8, R.drawable.scientist_9, R.drawable.scientist_10, R.drawable.scientist_11, R.drawable.scientist_12, R.drawable.scientist_13, R.drawable.scientist_14, R.drawable.scientist_15, R.drawable.scientist_16, R.drawable.scientist_17, R.drawable.scientist_18, R.drawable.scientist_19, R.drawable.scientist_20, R.drawable.scientist_21};
    }


    class MyViewHolder {
        ImageView myImages;
        TextView myNames;
        TextView myDescriptions;

        MyViewHolder(View v) {
            myImages = (ImageView) v.findViewById(R.id.imageView);
            myNames = (TextView) v.findViewById(R.id.textView1);
            myDescriptions = (TextView) v.findViewById(R.id.textView2);
        }
    }

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

        View row = convertView;
        MyViewHolder holder = null;

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        // first check to see if the view is null. if so, we have to inflate it.
        // to inflate it basically means to render, or show, the view.
        if (row == null) {
            row = inflater.inflate(R.layout.single_row, parent, false);
            holder = new MyViewHolder(row);

            row.setTag(holder);
            Log.d("App", "Creating a new row");

        } else {
            holder = (MyViewHolder) row.getTag();
            Log.d("App", "Recycling stuff");
        }
        // Capture position and set to the TextViews
        holder.myImages.setImageResource(images[i]);
        holder.myNames.setText(nameArray[i]);
        holder.myDescriptions.setText(descriptionArray[i]);

        return row;
    }

    // I don't know how can I do this part below
    protected void onItemClick(AdapterView<?> parent, View view, final int position, long id){

        row.setOnClickListener(new OnClickListener() {

            @Override
            public void ClickMe(View v) {

                //Click on listView row
                switch (MyAdapter[position].type) {
                    case TYPE_ITEM1:
                            res = R.layout.scientist_1;
                        break;
                    case 1:
                            res = R.layout.scientist_2;
                        break;
                    case 2:
                       // ........

                        break;
                    default:
                }

            }

        });
        }
    }         
}

@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_main, 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);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您知道inflate有多少布局,可以使用以下方法:

getViewTypeCount() - 此方法返回列表中有多少行类型

getItemViewType(int position) - 根据位置

返回应使用的布局类型

请参阅以下链接以获取更多信息:

http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html

http://android.amberfog.com/?p=296