안 드 로 이 드 에서 ListView 가 그림 과 글 의 병렬 을 실현 하고 분할 선 을 사용자 정의 합 니 다(모방 위 챗 앱 보완)

어제(오늘 새벽)블 로그'안 드 로 이 드 에서 Fragment 와 ViewPager 그 정도 의 일'에서 우 리 는 Fragment 와 ViewPager 모방 을 통 해 위 챗 의 레이아웃 구 조 를 실현 했다.오늘 우 리 는 ListView 를 사용 하여 연락처 란 의 기본 보 기 를 실현 합 니 다.효 과 는 다음 과 같 습 니 다.

위의 그림 의 효 과 를 실현 하려 면 우 리 는 두 가지 지식 점 을 사용 해 야 한다.
1.그림 목록 을 사용자 정의 로 만 들 수 있 습 니 다.(물론 Simple Adapter 도 사용 할 수 있 습 니 다.)
BaseAdapter(추상 류)사용자 정의 어댑터 를 계승 하면 더욱 유연 하고 복잡 한 목록 을 만 들 수 있 습 니 다.
사용자 정의 어댑터 ListView 최적화:
(1)고정 너비 사용(matchparent)의 ListView 는 item(ListView 의 줄 마다 레이아웃)을 채 울 때 ListView 구성 요 소 를 중복 렌 더 링 하지 않 고 getView 방법 을 여러 번 호출 하 는 데 도움 이 됩 니 다.
(2)Convertview 는 숨겨 진 item 대상 을 반복 적 으로 사용 하여 각 item 의 view 대상 을 반복 적 으로 만 들 지 않도록 합 니 다.
(3)ViewHolder 를 사용 하여 용기 에서 구성 요 소 를 찾 는 효율 을 최적화 시킨다.
2.ListView 사용자 정의 분할 선
기본 값 으로 분할 선(Divider)을 추가 합 니 다.그러나 이 기본 분할 선 은 너무 못 생 겨 서 저 희 는 사용자 정의 분할 선 스타일 로 사용자 체험 을 향상 시 켜 야 합 니 다.여기 서 두 가지 방법 을 소개 합 니 다.
(1)layot 에 있 는 ListView 구성 요소 에 android:divider="\#FFFF"를 추가 한 다음 ListView 에 대응 하 는 item 레이아웃 에 View 구성 요 소 를 사용 하여 분할 선 을 추가 합 니 다(다음 세대 에서 보 여 줍 니 다)
(2)drawable 에 inset 파일 을 만들어 분할 선의 위치 와 색상 을 정의 하고 values 의 diments.xml 에 속성 정의 분할 선의 굵기(dp)를 추가 합 니 다.
drawable 인 셋 파일 만 들 기:

 <?xml version="1.0" encoding="utf-8"?>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
   android:insetLeft="5dp"
   android:insetRight="5dp"
   android:drawable="@color/colorline">
 </inset>
inset 속성:삽 입 된 그리 기 가능 한 자원 을 정의 합 니 다.그것 은 반드시 뿌리 원소 여야 한다.속성 은 다음 과 같 습 니 다(ATTRIBUTES):
xmlns:android 문자열 값,필요 합 니 다.XML 의 네 임 스페이스 를 정의 합 니 다.
android:drawable:그 릴 자원 입 니 다.필요 합 니 다.삽입 할 자원 을 가리 키 고 있 습 니 다.
android:insetTop:사이즈 값.상단 의 끼 워 넣 은 위 치 를 크기 나 Dimension 자원 으로 정의 합 니 다.
android:insetRight:사이즈 값.오른쪽 에 있 는 삽입 위 치 를 크기 나 Dimension 자원 으로 정의 합 니 다.
android:insetBottom:사이즈 값.아래쪽 에 있 는 삽입 위 치 를 크기 나 Dimension 자원 으로 정의 합 니 다.
android:insetLeft:사이즈 값.왼쪽 에 있 는 삽입 위 치 를 크기 나 Dimension 자원 으로 정의 합 니 다.
values 의 diments.xml 파일 은 다음 과 같 습 니 다(listitem_line 은 사용자 정의 분할 선 굵기 입 니 다):

 <resources>
   <!-- Default screen margins, per the Android Design guidelines. -->
   <dimen name="activity_horizontal_margin">dp</dimen>
   <dimen name="activity_vertical_margin">dp</dimen>
   <dimen name="list_item_line">dp</dimen>
 </resources>
ListView 의 성명 방식(divider 와 divider Height 속성 을 각각 drable 과 diments 에서 사용자 정의 스타일 로 설정 합 니 다):

 <ListView
   android:id="@+id/contact_list"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:layout_alignParentTop="true"
   android:layout_alignParentStart="true"
   android:divider="@drawable/list_item_divider"
   android:dividerHeight="@dimen/list_item_line"/>
이상 은 우리 가 이번에 주로 사용 한 지식 입 니 다.다음은 어제 의 코드 를 계속 하고 전체적인 효과 시범 을 보 여 드 리 겠 습 니 다.
STEP 1:어제 layot 에서 contactlist 재 구성fragment.xml 파일:
ListView 구성 요소 에 android:divider="\#FFFF"를 추가 하여 기본 분할 선 을 보이 지 않 게 합 니 다.

 <?xml version="." encoding="utf-"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical">
   <TextView
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="     "
     android:textSize="sp"/>
   <ListView
     android:id="@+id/contact_list"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_alignParentStart="true"
     android:divider ="#FFFFFF"/>
 </LinearLayout>

두 번 째 단계:layot 에 listView 에 대응 하 는 item 레이아웃 파일 contactlist 추가item.xml
사용자 정의 분할 선 에 View 구성 요 소 를 추 가 했 습 니 다.
 

 <?xml version="." encoding="utf-"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical" android:layout_width="match_parent"
   android:layout_height="match_parent">
   <LinearLayout
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:padding="dp"
     android:orientation="horizontal">
     <ImageView
       android:id="@+id/friend_iv"
       android:layout_width="dp"
       android:layout_height="dp"
       android:background="@mipmap/ic_launcher" />
     <TextView
       android:id="@+id/friend_tv"
       android:layout_width="match_parent"
       android:layout_height="dp"
       android:layout_marginLeft="dp"
       android:gravity="center_vertical"
       android:text="wu"
       android:textSize="dp" />
   </LinearLayout>
   <View
     android:layout_width="match_parent"
     android:layout_height="dp"
     android:layout_marginLeft="dp"
     android:layout_marginRight="dp"
     android:background="@color/colorline"/>
 </LinearLayout>
세 번 째 단계:어제 자바 에 대응 하 는 ContactListFragment.java 파일 다시 쓰기

 import android.content.Context;
 import android.os.Bundle;
 import android.support.annotation.Nullable;
 import android.support.v.app.Fragment;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.BaseAdapter;
 import android.widget.ImageView;
 import android.widget.ListView;
 import android.widget.TextView;
 /**
 * Created by panchengjia on //.
 */
 public class ContactListFragment extends Fragment {
   ListView contact_list;
   String[] names = {"  ", "   ", "  ", "  ", "  ", "  ", "  ", "  ", "   ", "  ", "   ", "  ",
       "  ", "  ", "  ", "  ", "  ", "  ", "   "};
   int[] icons = {R.mipmap.guojia, R.mipmap.huangyueying, R.mipmap.huatuo,
       R.mipmap.liubei, R.mipmap.luxun, R.mipmap.lvbu, R.mipmap.lvmeng,
       R.mipmap.machao, R.mipmap.simayi, R.mipmap.sunquan, R.mipmap.sunshangxiang,
       R.mipmap.xiahoudun, R.mipmap.xuchu, R.mipmap.yangxiu, R.mipmap.zhangfei,
       R.mipmap.zhaoyun, R.mipmap.zhenji, R.mipmap.zhouyu, R.mipmap.zhugeliang};
   @Nullable
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
     View view = inflater.inflate(R.layout.contactlist_fragment,container,false);
     contact_list= (ListView) view.findViewById(R.id.contact_list);
     contact_list.setAdapter(new MyListViewAdapter(getContext()));
     return view;
   }
   class MyListViewAdapter extends BaseAdapter{
     Context context;
     public MyListViewAdapter(Context context) {
       this.context = context;
     }
     @Override
     public int getCount() {
       return names.length;
     }
     @Override
     public Object getItem(int position) {
       return names[position];
     }
     @Override
     public long getItemId(int position) {
       return position;
     }
     @Override
     public View getView(int position, View convertView, ViewGroup parent) {
       ViewHold vh;
       if (convertView == null) {
         convertView = LayoutInflater.from(context).inflate(R.layout.contactlist_item, null);
         vh = new ViewHold();
         vh.iv = (ImageView) convertView.findViewById(R.id.friend_iv);
         vh.tv = (TextView) convertView.findViewById(R.id.friend_tv);
         convertView.setTag(vh);
       }
       vh = (ViewHold) convertView.getTag();
       vh.iv.setImageResource(icons[position]);
       vh.tv.setText(names[position]);
       return convertView;
     }
     class ViewHold {
       ImageView iv;
       TextView tv;
     }
   }
 }

오늘 은 여기까지 입 니 다.내일 우 리 는 이 레이아웃 기능 을 계속 확장 하고 잠 을 잘 것 입 니 다.
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기