如何以垂直顺序以编程方式对视图进行充气

时间:2015-06-17 04:52:48

标签: java android android-layout

我刚接受Android开发。我决定制作一个ToDo列表(简单并且有一些视觉上的身体),但我面临着按顺序膨胀布局的问题。

基本上我有一个Activity,它收集应该在Main Activity中显示的信息。我得到其中一个出现,但无法弄清楚如何添加其中一个以上。正如标题所说,它们将以垂直顺序出现。

主:

package com.example.victor.todo;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewManager;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class MainActivity extends ActionBarActivity {

String NEW_TASK, NEW_TASK_DETAILS;
int pos = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

@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;
    }

    if (id == R.id.action_add_task){
        Intent i =  new Intent(this,AddTaskActivity.class);
        startActivityForResult(i, 1);
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 1){
        if(resultCode == RESULT_OK){
            String task = data.getStringExtra("task");
            String taskDetails = data.getStringExtra("details");

            String taskInfo = data.getStringExtra(AddTaskActivity.EXTRA_NAME);

            Log.i("TAG", "task: " + task);
            Log.i("TAG", "task info: " + taskDetails);

            AddTask(task, taskDetails);

        } else {
            //DO SOMETHING IF NO INFO IS RETURNED
        }
    }
}
public void AddTask(String task, String details){
    TextView tw = (TextView)findViewById(R.id.addMainHint);
    ((ViewManager) tw.getParent()).removeView(tw);

    LinearLayout linearLayoutMain = (LinearLayout) findViewById(R.id.mainLinearLayout);

    View v = getLayoutInflater().inflate(R.layout.task_listing, null);

    linearLayoutMain.addView(v);

    TextView taskName = (TextView) v.findViewById(R.id.single_task_info);
    taskName.setText(task);

    CheckBox taskCheckBox = (CheckBox) v.findViewById(R.id.single_task_checkbox);
    taskCheckBox.setChecked(false);

    /*
    RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.single_task_layout);
    relativeLayout.addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
    */

    /*


    RelativeLayout layout = (RelativeLayout) findViewById(R.id.single_task_layout);

    TextView ll = new TextView();
    */

}

public void TaskClicked(){

}

 }

task_listing.xml(充当信息容器的相对布局):

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

    android:id="@+id/single_task_layout"
    >

    <TextView
        android:layout_width="300dp"
        android:layout_height="30dp"
        android:id="@+id/single_task_info"
        android:layout_marginTop="20dp"
        android:textColor="@color/primary_material_dark"
        android:textSize="20sp"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="40dp"
        android:paddingLeft="15dp"
        android:paddingRight="15dp"
        android:clickable="true" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/single_task_checkbox"
        android:layout_gravity="left"
        android:layout_toLeftOf="@id/single_task_info"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="15dp"
        android:enabled="true"
        android:clickable="true"
        android:onClick="TaskClicked"
        android:visibility="visible" />

</RelativeLayout>

我想我使用的是错误的工具,但我甚至不知道要搜索什么!我没有Java本身的问题,但设计给我带来了严重的麻烦。我一直在试图解决这个问题几个小时(早上是7.48)!

同样,如果我只添加其中一个布局,那么其他javas和xmls可能不那么重要。

谢谢!

2 个答案:

答案 0 :(得分:2)

使用listview是正确的。尝试这样做(比你有的更精致,但值得。

创建一个实现Pacelable的Task对象。可以在AddTaskActivity

的结果中发回此对象
public class Task implements Parcelable {
private String myTask;
private String myDetails;

public static final Parcelable.Creator<Task> CREATOR
        = new Parcelable.Creator<Task>() {
    public Task createFromParcel(Parcel in) {
        return new Task(in);
    }

    public Task[] newArray(int size) {
        return new Task[size];
    }
};

public Task() {}

public Task(Parcel in){
    myTask = in.readString();
    myDetails = in.readString();
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(myTask);
    dest.writeString(myDetails);
}

}

这是你的课程略有修改

public class MainActivity extends ActionBarActivity {
private Adapter myAdapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myAdapter = new Adapter();
    ListView listView = (ListView)findViewById(android.R.id.list);
    listView.setAdapter(myAdapter);
}

@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;
    }

    if (id == R.id.action_add_task){
        Intent i =  new Intent(this,AddTaskActivity.class);
        startActivityForResult(i, 1);
    }

    return super.onOptionsItemSelected(item);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    if(requestCode == 1){
        if(resultCode == RESULT_OK){
            myAdapter.addTask(data.getParcelableExtra(AddTaskActivity.[key constant]));

        } else {
            //DO SOMETHING IF NO INFO IS RETURNED
        }
    }
}

public void TaskClicked(){

}

private class Adapter extends BaseAdapter {

    private List<Task> myTasks;

    public Adapter() {
        myTasks = new ArrayList<Task>();
    }

    public void addTask(Task task) {
        myTasks.add(task);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return myTasks.size();
    }

    @Override
    public Object getItem(int position) {
        return myTasks.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        TextView tw = (TextView)findViewById(R.id.addMainHint);
        ((ViewManager) tw.getParent()).removeView(tw);

        convertView = getViewNotNull(convertView, parent);

        Task task = (Task)getItem(position);

        TextView taskName = (TextView) convertView.findViewById(R.id.single_task_info);
        taskName.setText(task.getTask());
    }

    private View getViewNotNull(View convertView, ViewGroup parent) {
        if(convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.task_listing)

            //here you should create a viewholder and set it on the view using setTag()
        }
        return convertView;
    }
}

}

我很快把它扔到了一起,所以希望它有你需要的东西 你仍然需要在列表视图上实现项目点击监听器以及复选框的检查状态

答案 1 :(得分:1)

使用ListView轻松完成。