我有一個單行包含TextView
和EditText
,最初EditText
是看不見的,當對listview
單列用戶水龍頭然後EditText上變得可見,如果EditText上是可見的指定然後當用戶再次點擊,然後EditText變得不可見。Scrolling的問題列表視圖時單列包含的TextView和EditText上
當用戶點擊時,我可以切換到EditText,但是我無法知道EditText何時可見,用戶Scrool然後其他EditText對於其他某個位置也變得可見,而滾動則不可見由用戶點擊該位置。
public class TypeOfTruckAdapter extends BaseAdapter{
List<TypeTruckPogo> list;
Context context;
LayoutInflater inflater;
public String[] Current;
TypeTruckPogo typeTruckPogo;
public static HashMap<Integer,String> truckHashMap=new HashMap<Integer,String>();
public TypeOfTruckAdapter(Context context,List<TypeTruckPogo> list) {
this.list = list;
this.context = context;
for(int i=0;i<list.size();i++)
{
truckHashMap.put(i,"");
}
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
class Viewholder{
TextView name;
EditText nmbrOfTruck;
int ref;
Viewholder(View view) {
name = (TextView) view.findViewById(R.id.textView1);
nmbrOfTruck = (EditText) view.findViewById(R.id.et_nmbr_of_truck_id);
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Viewholder holder;
typeTruckPogo = list.get(position);
if(convertView==null){
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.single_row_for_type_of_truck, parent, false);
holder = new Viewholder(convertView);
convertView.setTag(holder);
}else{
holder = (Viewholder)convertView.getTag();
}
// For position zero i have to set the text color to blue color,else dark gray color
if(position==0){
holder.name.setTextColor(context.getResources().getColor(R.color.blue_color));
}else{
holder.name.setTextColor(context.getResources().getColor(R.color.darkgray));
}
// setting the name on the textview
holder.name.setText(list.get(position).getTypeOfTruckName());
//setting the tag on edittext
holder.nmbrOfTruck.setTag(position);
//setting the viewholder position
holder.ref = position;
// clicking on listview making edittext to appear (initially edittext is invisiable)
// if edittext is visiable make it invisiable and if it is invisiable make it visiable
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.findViewById(R.id.et_nmbr_of_truck_id).getVisibility() == View.VISIBLE) {
v.findViewById(R.id.et_nmbr_of_truck_id).setVisibility(View.INVISIBLE);
} else {
v.findViewById(R.id.et_nmbr_of_truck_id).setVisibility(View.VISIBLE);
}
}
});
// if user write on editText save the input by the user in specified position
//truckHashMap is hashmap where I saving the position as a key and value as the user Input
holder.nmbrOfTruck.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start,
int before, int count) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
// TODO Auto-generated method stub
}
public void afterTextChanged(Editable s) {
Current = new String[holder.ref];
truckHashMap.put(position, s.toString().trim());
}
});
// Setting the User Input at specified Position
holder.nmbrOfTruck.setText(truckHashMap.get(position));
Config.colorFont(context, null, holder.name, null);
return convertView;
}
// single_row_for_type_of_truck
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ll4"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
<TextView
android:id="@+id/textView1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center|left"
android:layout_gravity="center"
android:layout_weight=".9"
android:padding="@dimen/text_padding"
android:text="TextView" />
<EditText
android:id="@+id/et_nmbr_of_truck_id"
android:layout_width="50dp"
android:layout_height="25dp"
android:padding="2dp"
android:background="@drawable/rounded_edge_edittext"
android:textSize="14dp"
android:visibility="invisible"
android:layout_marginRight="10dp"
android:gravity="left"
android:saveEnabled="true"
android:numeric="integer"
android:inputType="number"
android:layout_weight=".1"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="@color/lightgray"/>
</LinearLayout>
請發佈您的'single_row_for_type_of_truck.xml'文件 –
我已經發布了代碼。 –