안녕하세요

리스트 안에 마퀴효과와 버튼 이벤트 질문입니다.

 

main.xml

 <?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="vertical" >

 <ListView
     android:id="@+id/list"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     />
</LinearLayout>


 

listcontent.xml

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="60dip"
    android:padding="5dip" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_toRightOf="@id/img"
        android:textColor="#00ff00"
        android:textSize="26sp"
        android:layout_marginRight="60dip"
        android:singleLine="true"
  android:ellipsize="end" />

    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:duplicateParentState="false"
        android:text="클릭" />

</RelativeLayout>


 

소스파일..

 public class ASeminar2Activity extends Activity {
 ArrayList<MyItem> arItem;
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  
  arItem = new ArrayList<MyItem>();
  MyItem mi;
  mi = new MyItem(R.drawable.ic_launcher, "장문테스트 장문테스트 장문테스트 장문테스트 장문테스트 장문테스트 장문테스트 장문테스트 장문테스트 장문테스트");arItem.add(mi);
  mi = new MyItem(R.drawable.ic_launcher, "단문테스트01");arItem.add(mi);
  mi = new MyItem(R.drawable.ic_launcher, "단문테스트02");arItem.add(mi);
  
  MyListAdapter MyAdapter = new MyListAdapter(this, R.layout.listcontent, arItem);
  ListView MyList;
  MyList=(ListView)findViewById(R.id.list);
  MyList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
  MyList.setAdapter(MyAdapter);
    
  MyList.setOnItemLongClickListener(new OnItemLongClickListener() {
   public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { 
    TextView txt = (TextView)findViewById(R.id.text);
    txt.setSelected(true);
    txt.setEllipsize(TruncateAt.MARQUEE);
    return false;
   }
  });
  
  MyList.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
     long arg3) {
    // TODO Auto-generated method stub
    Toast.makeText(ASeminar2Activity.this, "text", Toast.LENGTH_SHORT).show();
    TextView txt = (TextView)findViewById(R.id.text);
    //txt.setSelected(true);
    txt.setEllipsize(TruncateAt.END);
   }
  });  
 }
}
 
//리스트 뷰에 출력할 항목
class MyItem {
 MyItem(int aIcon, String aName) {
  Icon = aIcon;
  Name = aName;
 }
 int Icon;
 String Name;
}
//어댑터 클래스
class MyListAdapter extends BaseAdapter {
 Context maincon;
 LayoutInflater Inflater;
 ArrayList<MyItem> arSrc;
 int layout;
 
 public MyListAdapter(Context context, int alayout, ArrayList<MyItem> aarSrc) {
  maincon = context;
  Inflater = (LayoutInflater)context.getSystemService(
    Context.LAYOUT_INFLATER_SERVICE);
  arSrc = aarSrc;
  layout = alayout;
 }
 
 public int getCount() {
  return arSrc.size();
 }
 
 public String getItem(int position) {
  return arSrc.get(position).Name;
 }
 
 public long getItemId(int position) {
  return position;
 }
 
 // 각 항목의 뷰 생성
 public View getView(int position, View convertView, ViewGroup parent) {
  final int pos = position;
  if (convertView == null) {
   convertView = Inflater.inflate(layout, parent, false);
  }
  ImageView img = (ImageView)convertView.findViewById(R.id.img);
  img.setImageResource(arSrc.get(position).Icon);
  
  TextView txt = (TextView)convertView.findViewById(R.id.text);
  txt.setText(arSrc.get(position).Name);
  //txt.setSelected(true);
  
  Button btn = (Button)convertView.findViewById(R.id.btn);
  btn.setFocusable(false);
  btn.setTag(position);
  btn.setOnClickListener(new Button.OnClickListener() {
   public void onClick(View v) {
    String str = arSrc.get(pos).Name + "를 눌렀습니다.";
    Toast.makeText(maincon, str, Toast.LENGTH_SHORT).show();
   }
  });
  return convertView;
 }
}

 

 


 

리스트 안에 커스텀뷰로 나타낸 것입니다.

다들 한번씩 접해 보셨겠죠?

제가 궁금한것은요.

 

1. 리스트를 클릭 했을때 버튼도 같이 포커스를 받아 클릭이 된다는점. (버튼은 클릭 안되게)

2. 리스트를 클릭하고 있을경우 마퀴 효과를 넣었는데요..  2번 리스트 아이템을 클릭하고 있을면(롱클릭) 1번에서 움직인다는 점입니다.

해당 아이템 부분에서만 움직이게 할 수 없는지요?

 

아직 안드로이드를 접한지 얼마 안되어서 어렵네요 ^^

아시는 분 좀 알려주세요..