我是一个新手android.I我的迷你应用程序有一些问题。 你可以看到下图: http://i481.photobucket.com/albums/rr175/viethungit/android.png
首先,第1行(布局1)出现listview时出现第1行,第2行(布局2)中的单击按钮,然后继续...单击按钮添加第1行,布局2出现在列表视图中.... 我尝试从Mr.Google搜索,但我找不到... 任何人都可以帮助我!
这是布局和活动我不知道如何实现
add_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:orientation="horizontal"
android:padding="5dip">
<!-- Image Item-->
<ImageButton
android:id="@+id/imgItem"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:contentDescription="@string/imgView" />
<!-- Name item -->
<EditText
android:id="@+id/edtItem"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/imgItem"
android:background="@drawable/bg"
android:hint="@string/txtTitle"
android:textSize="20dip" />
<!-- Button add -->
<Button
android:id="@+id/btnAdd"
android:background="@drawable/add"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
<!-- item -->
<TextView
android:id="@+id/txtSubtitle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_toLeftOf="@+id/btnAdd"
android:gravity="right|center_vertical"
android:layout_centerVertical="true"
android:textSize="20dip"
android:text="@string/txtSubtitle" />
</RelativeLayout>
plus_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:orientation="horizontal"
android:padding="5dip" >
<!-- Image Item -->
<ImageView
android:id="@+id/imgItem"
android:src="@drawable/chomsao"
android:layout_width="40dip"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="2dp"
android:contentDescription="@string/imgView" />
<!-- Name Item -->
<TextView
android:id="@+id/edtItem"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@+id/imgItem"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:text="@string/txtTitle"
android:textSize="20dip" />
<!-- Quantity Item -->
<TextView
android:id="@+id/txtQtyItem"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_toRightOf="@id/edtItem"
android:layout_marginLeft="20dp"
android:gravity="center_vertical"
android:textSize="20dip"
android:text="@string/QuantityItem"/>
<!-- Button plus -->
<Button
android:id="@+id/btnPlus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="@drawable/plus" />
<!-- Button minus -->
<Button
android:id="@+id/btnMinus"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_alignBaseline="@id/btnPlus"
android:layout_toLeftOf="@+id/btnPlus"
android:background="@drawable/minus" />
<!-- Price Item -->
<TextView
android:id="@+id/txtSubtitle"
android:layout_width="100dp"
android:layout_height="40dp"
android:layout_centerVertical="true"
android:layout_toLeftOf="@+id/btnMinus"
android:gravity="right|center_vertical"
android:text="@string/txtSubtitle"
android:textSize="20dip" />
</RelativeLayout>
activity_main.xml
<RelativeLayout
android:id="@+id/relative1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/relative"
android:layout_above="@id/relative2"
android:background="#EEEEEE"
android:layout_marginLeft="1.5dp"
android:layout_marginRight="1.5dp"
android:layout_marginTop="1.5dp">
<ListView
android:id="@+id/listSale"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
答案 0 :(得分:10)
public class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
private ImageView myImage;
public static String upload= " ";
public static String GalleryImage;
public ArrayList<ListItem> myItems = new ArrayList<ListItem>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listandimage);
myList = (ListView) findViewById(R.id.MyList);
myImage= (ImageView)findViewById(R.id.image1);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
ListItem listItem = new ListItem();
listItem.textdata="@";
listItem.caption = "";
myItems.add(listItem);
myList.setAdapter(myAdapter);
myAdapter.notifyDataSetChanged();
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public MyAdapter() {
mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return myItems.size();
}
public ListItem getItem(int position) {
return myItems.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.text=(TextView )convertView.findViewById(R.id.textView1);
holder.captionEditText = (EditText) convertView.findViewById(R.id.ItemCaption);
holder.addOrDeleteButton = (Button) convertView.findViewById(R.id.buttonAdd);
holder.captionEditText.setFocusable(true);
holder.captionEditText.requestFocus();
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
// Fill EditText with the value you have in data source
// holder.captionEditText.setId(position);
holder.text.setTag(position);
holder.captionEditText.setTag(position);
holder.captionEditText.setText(getItem(position).caption);
holder.addOrDeleteButton.setTag(position);
// / this updates tag of
// the button view as we
// scroll ///
holder.addOrDeleteButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
int tag = (Integer) view.getTag();
if (tag != (myItems.size() - 1)) {
myItems.remove(tag);
Log.d("GCM", "Item removed from " + tag);
myAdapter.notifyDataSetChanged();
} else {
ListItem listItem = new ListItem();
listItem.textdata="@";
listItem.caption = "";
myItems.add(listItem);
/*
* Log.d("GCM", holder.captionEditText.getText()
* .toString()); myItems.get((Integer)
* view.getTag()).caption = holder.captionEditText
* .getText().toString();
*/
myAdapter.notifyDataSetChanged();
myList.setSelection(myAdapter.getCount() - 1);
// holder.captionEditText.setFocusable(true);
// holder.captionEditText.requestFocus();
}
}
});
holder.captionEditText.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
// TODO Auto-generated method stub
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
// if(position < myItems.size())
// getItem(position).caption = s.toString();
myItems.get((Integer) holder.captionEditText.getTag()).caption = holder.captionEditText
.getText().toString();
}
});
if (position != (myItems.size() - 1)) {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttarecloseicon);
} else {
holder.addOrDeleteButton.setBackgroundResource(R.drawable.fruttareaddicon);
holder.text.setFocusable(true);
holder.captionEditText.setFocusable(true);
holder.text.requestFocus();
holder.captionEditText.requestFocus();
}
return convertView;
}
}
class ViewHolder {
TextView text;
EditText captionEditText;
Button addOrDeleteButton;
}
class ListItem {
String textdata;
String caption;
}
}
使用此代码根据您的需要进行编辑。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView1"
android:layout_width="15dp"
android:layout_height="50dp"
android:text=" @"
/>
<EditText
android:id="@+id/ItemCaption"
android:layout_width="270dp"
android:layout_height="50dp"
android:layout_margin="3dip"
android:imeOptions="actionDone|flagNoExtractUi"
android:inputType="textNoSuggestions"
android:singleLine="true" >
</EditText>
<Button
android:id="@+id/buttonAdd"
android:layout_width="75dp"
android:layout_height="50dp"
android:layout_margin="3dip"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#fff"
android:orientation="horizontal" >
<ListView
android:id="@+id/MyList"
android:layout_width="370dp"
android:layout_height="160dp"
android:layout_gravity="center|top"
android:layout_marginLeft="150dp"
android:descendantFocusability="beforeDescendants"
>
</ListView>
<ImageView
android:id="@+id/image1"
android:layout_width="80dp"
android:layout_height="80dp"
/>
</LinearLayout>
如果有用,请将答案标记为正确!!
答案 1 :(得分:0)
您可以创建元素(如按钮,视图等)并将它们添加到现有视图中,如:
myListView.addChild(CreatedButton);