1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【質問です!】スクロール時にListviewの要素が保持できない!

Posted at

ListViewをAdapterで動的にカスタマイズしています。

中の要素をある程度保てるようになったのですが、
AdapterのgetView内でフラグを立ててsetVisibilityのGONEとVisibleを分岐させている部分が保持できません。

最初は良いのですが、スクロールして戻ってくると表示が変わってしまいます。
解決する方法をご存知の方がみえましたら、ご教示くださるとありがたいです。

検証中のコードのため、不要な部分もあると思いますが、ご容赦ください。
よろしくお願いします。

activity_main.xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:paddingTop="25dp"
    tools:context=".MainActivity"
    android:fillViewport="false"
    android:layout_width="270dp"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="0dp"
        android:paddingRight="0dp"
        android:paddingBottom="0dp"
        android:paddingTop="0dp" >
        
        <ListView
            android:id="@+id/listView_r"
            android:layout_width="270dp"
            android:layout_height="match_parent" >
        </ListView>

    </LinearLayout>
</RelativeLayout>

list_item.xml


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="270dp"
    android:layout_height="160dp"
    android:alpha="1"
    android:id="@+id/frame_back"
    android:background="@drawable/frame_back_on"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <TextView
        style="@style/behindMenuItemLabel"
        android:id="@+id/frame_title"
        android:typeface="sans"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="16dp"
        android:layout_marginTop="15dp"
        android:layout_marginLeft="36dp"
        android:text="タイトル" />

    <Button
        android:src="@drawable/tap_color"
        android:focusableInTouchMode="false"
        android:id="@+id/frame_btn"
        android:focusable="false"
        android:background="#00FFFFFF"
        android:layout_width="270dp"
        android:layout_height="40dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_above="@+id/debug_text" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:id="@+id/frame_price"
        android:typeface="sans"
        android:textStyle="bold"
        android:gravity="center"
        android:textSize="12dp"
        android:layout_marginTop="120dp"
        android:layout_marginLeft="46dp"
        android:text="テキスト" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:id="@+id/frame_date"
        android:typeface="sans"
        android:textStyle="normal"
        android:gravity="center"
        android:textSize="10dp"
        android:layout_marginTop="122dp"
        android:layout_marginLeft="196dp"
        android:text="日付" />

    <TextView
        style="@style/behindMenuItemLabel"
        android:id="@+id/debug_text"
        android:typeface="sans"
        android:textStyle="normal"
        android:gravity="center"
        android:textSize="10dp"
        android:text="連番"
        android:layout_below="@+id/frame_title"
        android:layout_toRightOf="@+id/dlPageButton"
        android:layout_toEndOf="@+id/dlPageButton" />

    <HorizontalScrollView
        android:scrollbars="none"
        android:id="@+id/horizontalScrollView_r"
        android:layout_height="60dp"
        android:alpha="1"
        android:layout_marginLeft="25dp"
        android:layout_marginBottom="38dp"
        android:layout_width="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/horizontal_child"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/default_1"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/frame_default"
                android:layout_marginRight="5dp" />
            <ImageView
                android:id="@+id/default_2"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/frame_default"
                android:layout_marginRight="5dp" />
            <ImageView
                android:id="@+id/default_3"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/frame_default"
                android:layout_marginRight="5dp" />
            <ImageView
                android:id="@+id/default_4"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/frame_default"
                android:layout_marginRight="5dp" />
            <ImageView
                android:id="@+id/default_5"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@drawable/frame_default"
                android:layout_marginRight="5dp" />

            <ListView
                android:id="@+id/listView_thumb"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">
            </ListView>

      </LinearLayout>
    </HorizontalScrollView>

    <ImageButton
        android:src="@drawable/tap_color"
        android:focusableInTouchMode="false"
        android:focusable="false"
        android:background="@drawable/dl_btn"
        android:layout_width="106dp"
        android:layout_height="27dp"
        android:id="@+id/dlPageButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

RightListItemsAdapter.java


public class RightListItemsAdapter extends BaseAdapter {
    private Context context;
    private List<RightListItems> items;


    //フラグ
    public static List<String> dl_flag = new ArrayList<String>();
    public static String str_dl_flag;

    //番号保持用
    public static int frame_number;


    public RightListItemsAdapter(Context context, List<RightListItems> items) {
        this.context = context;
        this.items = items;
    }


    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        View customItemView = null;

        RightListItems item = items.get(position);


        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            customItemView = inflater.inflate(R.layout.right_list_items, parent, false);

            customItemView.setTag(listCount);
            Log.d(TAG, "Inflate " + listCount + "  (position: " + position + ")  " + customItemView.hashCode());
            listCount++;


        } else {
            customItemView = convertView;
        }


        TextView debugs = (TextView)customItemView.findViewById(R.id.debug_text);
        debugs.setText(String.valueOf(listCount));


        Object object = getItem(position);
        if(object != null) {

        TextView titleTextView = (TextView)customItemView.findViewById(R.id.frame_title);
        TextView priceTextView = (TextView)customItemView.findViewById(R.id.frame_price);
        TextView dateTextView = (TextView)customItemView.findViewById(R.id.frame_date);

        titleTextView.setText(item.getFrameTitle());
        priceTextView.setText(item.getFramePrice());
        dateTextView.setText(item.getFrameDate());

        int frameId = item.getFrameId();



        if(frameId == 1){
            ImageButton btn = (ImageButton)customItemView.findViewById(R.id.dlPageButton);
            btn.setVisibility(View.GONE);
        }

        //ここのフラグで表示を切り替えているのですが、スクロールすると表示がバラバラになってしまいます
        if( (item.getFrameDLFlag().equals("dl_still") )){

            HorizontalScrollView hsv = (HorizontalScrollView)customItemView.findViewById(R.id.horizontalScrollView_r);
            hsv.setVisibility(View.GONE);
            RelativeLayout layout = (RelativeLayout)customItemView.findViewById(R.id.frame_back);
            layout.setBackgroundResource( R.drawable.frame_back );

            debugs = (TextView)customItemView.findViewById(R.id.debug_text);
            debugs.setText(String.valueOf(listCount));


        }else if( item.getFrameDLFlag().equals("dl_end") ){
            ImageButton btn = (ImageButton)customItemView.findViewById(R.id.dlPageButton);
            btn.setVisibility(View.VISIBLE);
        }


//画像表示
        int dir_num = position+1;
        Bitmap bm;

        for( int j=1; j <= 5; j++ ) {
            int img_id = context.getResources().getIdentifier("default_" + j, "id", "プロジェクト名");
            ImageView imageView = (ImageView)customItemView.findViewById(img_id);

            try {
                InputStream is = context.getAssets().open(dir_num + "/frm_" + j + ".png");

                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(is, null, options);

                    int scaleW = options.outWidth / 100 ;
                    int scaleH = options.outHeight / 100 ;

                    int scale = Math.max(scaleW, scaleH);
                    options.inJustDecodeBounds = false;
                    options.inSampleSize = scale;

                    InputStream is_s = context.getAssets().open(dir_num + "/frm_" + j + ".png");
                    bm = BitmapFactory.decodeStream(is_s, null, options);

                    imageView.setImageDrawable(null);
                    imageView.setImageBitmap(null);
                    imageView.setImageBitmap(bm);


                is.close();

            } catch (IOException e) {
                Log.e("err", e.toString());
            }

        }




        }


//クリックイベント
        ImageButton bo = (ImageButton)customItemView.findViewById(R.id.dlPageButton);
        bo.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                int pos = position;
                ((ListView) parent).performItemClick(v, pos, (long) 0);
            }
        });

        Button frame_btn = (Button)customItemView.findViewById(R.id.frame_btn);
        frame_btn.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                int pos = position;
                ((ListView) parent).performItemClick(v, pos, (long) 1);
            }
        });


        return customItemView;
    }
}


RightListItems

public class RightListItems {
    private String frameTitle;
    private String framePrice;
    private String frameDate;
    private String frameDLFlag;
    private int frameId;

    private String str_dl_flag;
    boolean enabled;

    public String getFrameTitle() {
        return frameTitle;
    }

    public void setFrameTitle(String frameTitle) {
        this.frameTitle = frameTitle;
    }

    public String getFramePrice() {
        return framePrice;
    }

    public void setFramePrice(String framePrice) {
        this.framePrice = framePrice;
    }

    public String getFrameDate() {
        return frameDate;
    }

    public void setFrameDate(String frameDate) {
        this.frameDate = frameDate;
    }

    public int getFrameId() {
        return frameId;
    }

    public void setFrameId(int frameId) {
        this.frameId = frameId;
    }

    public String getFrameDLFlag() {
        return frameDLFlag;
    }

    public void setFrameDLFlag(String frameDLFlag) {
        this.frameDLFlag = frameDLFlag;
    }

    public String getListDLFlag() {
        return str_dl_flag;
    }

    public void setListDLFlag(String str_dl_flag) {
        this.str_dl_flag = str_dl_flag;
    }


    public boolean isEnabled(){
        return enabled;
    }

}




MainActivity.java



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        ArrayList<RightListItems> items = new ArrayList<RightListItems>();
        for (int i = 0; i < frame_id.size(); i++) {
            RightListItems item = new RightListItems();

            item.setFrameTitle(String.format(frame_name.get(i)));
            item.setFramePrice(String.format(frame_type.get(i)));
            item.setFrameDate(String.format(frame_new.get(i)));
            item.setFrameId(Integer.parseInt(frame_id.get(i)));

            item.setFrameDLFlag(String.format(dl_flag.get(i)));
            item.setListDLFlag(String.format(str_dl_flag));

            items.add(item);
        }
        RightListItemsAdapter adapterRightList = new RightListItemsAdapter(this, items);
        ListView listView = (ListView)findViewById(R.id.listView_r);
        listView.setAdapter(adapterRightList);
        listView.setOnItemClickListener(onItemClickListener);



}

//クリック時処理
private OnItemClickListener onItemClickListener = new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        if(id == 0) {


    //クリック時処理


        }else if(id == 1) {

        //クリック時処理

        }
    }
};

//関連性の低い部分は省略しています

1
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?