如何以编程方式膨胀意见垂直秩序 [英] How to inflate views programmatically in vertical order

查看:193
本文介绍了如何以编程方式膨胀意见垂直秩序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拿起Android的发展。我决定做一个待办事项列表(简单,有点视觉体),但我面临的问题,以便充气布局。

I just picked up Android developing. I decided to make a ToDo-list(simple and has a somewhat visual body) but I'm facing problems with inflating Layout in order.

基本上我有收集了应该在主要活动中显示的信息的活动。我让他们一个展示,但无法弄清楚如何添加其中的多个。他们会被显示出来的垂直顺序标题说。

Basically I have an Activity that collects information that is supposed to be shown in Main Activity. I get one of them showing up, but can't figure out how to add more than one of them. They'd be showing up in vertical order as the title says.

主营:

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(充当信息的容器中的相对布局):

task_listing.xml(a relative layout that acts as the container of the info):

   <?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上午)!

I guess I'm using the wrong tools but I don't even know what to search for! I don't have problems with Java itself but the design is giving me serious headache. I've been trying to figure this out for hours(it's 7.48 in the morning)!

此外,它工作,如果我补充一点的布局之一,所以其他的Java类和XML的并不可能是很重要的。

Again, it works if I add just one of the layouts so the other javas and xmls aren't probably that important.

谢谢!

推荐答案

使用一个ListView是正确的。试着这样做(有一点比你有什么更复杂的,但值得。

Use a listview is correct. Try doing this (A little bit more elaborate than what you have but worth it.

创建一个实现Pacelable任务对象。这个目的可以在AddTaskActivity的结果被发送回

Create a Task object that implements Pacelable. This object can be sent back in the result of 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;
    }
}

}

我赶紧扔了一起,所以希望它有你所需要的上手
你仍然需要实现列表视图的项目点击监听器以及用于复选框检查状态

I quickly threw this together so hopefully it has what you need to get started you'll still need to implement the item click listener on the listview as well as the check status for the checkboxes

这篇关于如何以编程方式膨胀意见垂直秩序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆