当我用Intent和startActivity()打开我的Activity(名为MovieViewActivity.java或.class)时,活动成功加载了gridView,但gridView只加载了第一个看不见的项目,它在屏幕的底部消失了
起初,我认为这是一个与内存泄漏有关的问题,但是当我快速上下滚动时,它会随着时间的推移加载剩余的项目。
我认为当一个项目从屏幕上消失时会被加载,我知道如何修复它。 请帮助我,以便我第一次看到所有项目,我加载视图...
这些是有关问题的图片。 (为了我的隐私,我删除了图片和文字,蓝色方块是图片(ImageView),黑色是文本(TextView)。)
When I load activity, only first item only shown
After I scroll up and down fastly, every items are appear.
这些是我的代码。
MainActivity.java的一部分(加载具有GridView的MovieViewActivity)
@Override
public void onClick (View view){
switch (view.getId()) {
case R.id.movieGallery:
showMovieGallery();
break;
}
}
public void showMovieGallery(){
Intent videoIntent = new Intent(this, MovieViewActivity.class);
startActivity(videoIntent);
}
MovieViewActivity.java的整个代码
package SubViewClass;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.l.workspace.R;
import org.w3c.dom.Text;
public class MovieViewActivity extends AppCompatActivity {
private int[] imgIDs = new int[]{
R.drawable.moviethumb0,
R.drawable.moviethumb1,
R.drawable.moviethumb2,
R.drawable.moviethumb3,
R.drawable.moviethumb4,
R.drawable.moviethumb5,
R.drawable.moviethumb6
};
private String[] imgTitles = new String[]{
"소개 영상", "교육 후기", "다양한 놀이 활동",
"영어로 놀아요", "영어 뮤지컬", "영어 요가", "영어 요리"
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video);
GridView movieGridView = (GridView)findViewById(R.id.movieGrid);
movieAdapter thumbnailGridAdapter = new movieAdapter(
this,
R.layout.griditem,
imgIDs, imgTitles);
movieGridView.setAdapter(thumbnailGridAdapter);
movieGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
showVideoWithPosition(position);
}
});
}
public void showVideoWithPosition(int position){
String movieName = "movie"+position;
Intent videoIntent = new Intent(this, VideoViewActivity.class);
videoIntent.putExtra("pos", movieName);
startActivity(videoIntent);
}
/* 그리드뷰 아이템을 위한 Adapter클래스 */
public class movieAdapter extends BaseAdapter{
private Context context;
private int[] img = null;
private String[] titles = null;
private LayoutInflater inf;
private int layout;
public movieAdapter(Context _context,int _layout, int[] _imgIDs, String[] _titles){
this.context = _context;
this.layout = _layout;
this.img = _imgIDs;
this.titles = _titles;
inf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount(){
return (img != null) ? img.length : 0;
}
public Object getItem(int position){
return (img != null) ? img[position] : 0;
}
public long getItemId(int position){
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
ImageView imgView;
TextView txtView;
if(convertView == null){
convertView = inf.inflate(layout, parent, false);
}else{
/* 값을 지정할 이미지뷰와 텍스트뷰를 각각 griditem에서 가져옴. */
imgView = (ImageView)convertView.findViewById(R.id.thumbnailImg);
txtView = (TextView)convertView.findViewById(R.id.thumbnailTxt);
/* GridView의 아이템을 구성할 ImageView와 TextView를 실제로 구성 */
imgView.setImageResource(img[position]);
txtView.setText(titles[position]);
}
return convertView;
}
}
}
MovieViewActivity的布局XML文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<GridView
android:id="@+id/movieGrid"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="2"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"></GridView>
</LinearLayout>
答案 0 :(得分:0)
在MovieAdapter类中进行以下更改。
public class MovieAdapter extends BaseAdapter {
private Context context;
private int[] img = null;
private String[] titles = null;
private LayoutInflater inf;
private int layout;
public MovieAdapter (Context context, int[] img, String[] titles, int layout) {
this.context = context;
this.img = img;
this.titles = titles;
this.layout = layout;
inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return (img != null) ? img.length : 0;
}
@Override
public Object getItem(int i) {
return i;
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
ViewHolder holder;
if (view == null) {
view = inf.inflate(R.layout.vault_item, null, false);
holder = new ViewHolder();
holder.imgView = (ImageView)convertView.findViewById(R.id.thumbnailImg);
holder.txtView = (TextView)convertView.findViewById(R.id.thumbnailTxt);
view.setTag(holder);
} else {
holder = (ViewHolder) view.getTag();
}
holder.imgView.setImageResource(img[i]);
holder.txtView.setText(titles[i]);
return view;
}
public static class ViewHolder {
private TextView txtView;
private ImageView imgView;
}
}