ListView उदाहरण में जाने से पहले, हमें ListView के बारे में पता होना चाहिए। ListView ArrayList, लिस्ट या किसी डेटाबेस से खींचे गए आइटम्स का संग्रह है। ListView का सबसे आम उपयोग वर्टिकल फॉर्मेट में आइटम्स का संग्रह है, हम ऊपर/नीचे स्क्रॉल कर सकते हैं और किसी भी आइटम पर क्लिक कर सकते हैं।
स्क्रॉल करते समय ListView का बैकग्राउंड काला होने से बचने का सरल समाधान यहां है।
चरण 1: Android Studio में नया प्रोजेक्ट बनाएं
File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: activity_main.xml में कोड जोड़ें
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
उपरोक्त activity_main.xml में हमने एक ListView घोषित किया है। स्क्रॉल करते समय ListView का बैकग्राउंड काला होने से बचने के लिए, हमें ListView में cacheColorHint जोड़ना होगा:
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"/>
चरण 3: MainActivity.java में कोड जोड़ें
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView list = findViewById(R.id.list);
ArrayList<String> arrayList = new ArrayList<>();
arrayList.add("JAVA");
arrayList.add("ANDROID");
arrayList.add("C Language");
arrayList.add("CPP Language");
arrayList.add("Go Language");
arrayList.add("AVN SYSTEMS");
// अधिक आइटम जोड़ें स्क्रॉल टेस्ट के लिए
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
list.setAdapter(arrayAdapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String clickedItem = (String) list.getItemAtPosition(position);
Toast.makeText(MainActivity.this, clickedItem, Toast.LENGTH_LONG).show();
}
});
}
}
आउटपुट

अब ListView स्क्रॉल करें, आपको कोई बैकग्राउंड कलर इश्यू नहीं मिलेगा।
