2017-12-09 13 views
0

カスタムリストビューについて学ぶためのアプリをコーディングしました。アプリにエラーはありませんが、私には1つの問題があります。私は次のように画像に示しました。 IMAGE 1 IMAGE 2カスタムリスト特定の画面の高さに1つの項目を表示

問題は、私は私はこの問題を解決するためにother.How以下のいずれかを表示されません。次のリストitem.Theリストの項目を見つけることができる前に長い道のりをスクロールする必要がありますか?

package com.example.hp.customlistview; 

import android.support.constraint.ConstraintLayout; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
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; 

public class MainActivity extends AppCompatActivity { 

    int IMAGES[]={R.drawable.adsense, R.drawable.allo, R.drawable.chrome, R.drawable.firebase, R.drawable.youtube}; 
    String[] NAMES={"AdSense","Allo","Chrome","Firebase","YouTube"}; 
    String [] DESCRIPTIONS={"Money through ads","Video calling","Web Browser","Cloud Database","Video Streaming"}; 
    private ListView listView; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     listView=(ListView)findViewById(R.id.listView); 
     CustomAdapter customAdapter=new CustomAdapter(); 
     listView.setAdapter(customAdapter); 
    } 

    class CustomAdapter extends BaseAdapter{ 

     @Override 
     public int getCount() { 
      Log.d("Length of Array ","The length is "+IMAGES.length); 
      return IMAGES.length; 
     } 

     @Override 
     public Object getItem(int i) { 
      return null; 
     } 

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

     @Override 
     public View getView(int i, View view, ViewGroup viewGroup) { 
      LayoutInflater layoutInflater=(LayoutInflater)getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE); 
      view=layoutInflater.inflate(R.layout.customlayout, null); 
      Log.d("Image ID","The id is "+R.id.imageView); 
      ImageView imageView=(ImageView)view.findViewById(R.id.imageView); 
      TextView textView_name=(TextView)view.findViewById(R.id.textView); 
      TextView textView_desc=(TextView)view.findViewById(R.id.textView2); 
      if(imageView==null) 
       Log.d("NULL?","YES IT IS NULL"); 
      else 
       Log.d("NULL?","NO IT IS NOT NULL"); 
      imageView.setImageResource(IMAGES[i]); 
      textView_name.setText(NAMES[i]); 
      textView_desc.setText(DESCRIPTIONS[i]); 
      Log.d("Hello","Hello there "+textView_name.getText().toString()); 
      return view; 
     } 
    } 
} 

customlayout.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="120dp"> 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="86dp" 
     android:layout_height="91dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginStart="32dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintStart_toStartOf="parent" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.0" 
     app:srcCompat="@mipmap/ic_launcher" /> 

    <TextView 
     android:id="@+id/textView" 
     android:layout_width="161dp" 
     android:layout_height="39dp" 
     android:layout_marginBottom="8dp" 
     android:layout_marginStart="28dp" 
     android:layout_marginTop="24dp" 
     app:layout_constraintBottom_toTopOf="@+id/textView2" 
     app:layout_constraintStart_toEndOf="@+id/imageView" 
     app:layout_constraintTop_toTopOf="parent" 
     app:layout_constraintVertical_bias="0.0" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="237dp" 
     android:layout_height="57dp" 
     android:layout_marginBottom="380dp" 
     android:layout_marginEnd="8dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginStart="44dp" 
     app:layout_constraintBottom_toBottomOf="parent" 
     app:layout_constraintEnd_toEndOf="parent" 
     app:layout_constraintHorizontal_bias="1.0" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintStart_toEndOf="@+id/imageView" /> 

    <CheckBox 
     android:id="@+id/checkBox" 
     android:layout_width="39dp" 
     android:layout_height="39dp" 
     android:layout_marginRight="16dp" 
     android:layout_marginTop="16dp" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintStart_toEndOf="@+id/textView" 
     app:layout_constraintTop_toTopOf="parent" /> 
</android.support.constraint.ConstraintLayout> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="com.example.hp.customlistview.MainActivity"> 

    <ListView 
     android:id="@+id/listView" 
     android:layout_width="386dp" 
     android:layout_height="409dp" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toTopOf="parent" /> 

    <Button 
     android:id="@+id/button" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginTop="24dp" 
     android:text="Ok" 
     app:layout_constraintLeft_toLeftOf="parent" 
     app:layout_constraintRight_toRightOf="parent" 
     app:layout_constraintTop_toBottomOf="@+id/listView" /> 
</android.support.constraint.ConstraintLayout> 

これを解決するために、私は次のよう

1)アンドロイドを試してみました:layout_height はここに私のコードです= "match_parent"を120 dpの特定の高さに設定します。 (それはcustomlayout.xmlである) しかし、それは

2)は、このような絶対的および相対的レイアウトなど、いくつかの他のレイアウトを、使用される動作しませんでしたが、イメージはあまり期待サイズ

EDITを超えて、リストコンテンツをいっぱいに。 私は、以下のコメントのベンP.の提案の後に、 のようにコンパクトな方法で表示されたリストアイテムを持っています。 IMAGE 3 しかし、私はMainActivity.javaでプログラムで設定していますが、明らかに表示されていません。 android:layout_heightをカスタム値に設定すると、プレビューで説明テキストの表示が消えます。

誰かがダウンワードしている場合は、その理由を教えてください。これは私の質問の質を向上させるのに役立ちます。

+0

です。各項目はリストビュー自体と同じ高さになります。あなたは固定された高さか 'wrap_content'を使うべきです –

+0

私は本当にこれが問題を解決するとは思っていませんが、あなたがcustomlayoutを膨張させるとき、あなたは親を取る呼び出しを使うべきです。 'view = layoutInflater.inflate(R.layout.customlayout、viewGroup、false);' –

+0

カスタムレイアウトのリストビューとビューに 'wrap_content'を使用します。それが役に立てば幸い。 – Chithra

答えて

0

私はそれを理解しました。将来類似の疑いがあるかもしれない人のために投稿する。 アイテムをドラッグ&ドロップする前でもレイアウトの高さを120dpまたは有限の値に設定します。 この縮小レイアウトでドラッグアンドドロップ操作を開始します。 かなり簡単ですね。これは私がそれを解決するのに役立ちました。 ここでは、あなたは間違いなくcustomlayoutの高さのために `match_parent`を使用する必要はありません:)

Image 5

関連する問題