GridView适配器无法使用Orientation更改

时间:2015-07-02 11:08:16

标签: android android-fragments gridview android-orientation

我一直在学习如何在android中使用GridView。我编写了一个示例类,它使用片段在网格视图中加载数据。我可以正确加载网格视图中的细节,但每当我更改方向时,我的网格视图都会变空。

PS:我在互联网上阅读 android:configChanges ,我不想用它。

这是我的代码:

public class MyActivity extends AppCompatActivity {

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

        // find the fragment on activity restarts
        FragmentManager fm = getSupportFragmentManager();
        MyFragment fragment = (MyFragment) fm.findFragmentByTag("my-frag");

        // create the fragment and data the first time
        if (fragment == null) {
            // add the fragment
            fragment = new MyFragment();
            fm.beginTransaction().add(R.id.container, new MyFragment(), "my-frag").commit();
        } else {
            // load the data from the results
            fragment.loadData();
        }
    }

}

片段类

/**
 * A placeholder fragment containing a simple view.
 */
public class MyFragment extends Fragment {

    public MyFragment() {
    }

    private GridView resultGridView;
    private MyAdapter myAdapter;

    private String[] urls = new String[]{"https://www.gravatar.com/avatar/38e4dbce6996ba5d73c56bee4297d385?s=328&d=identicon&r=PG&f=1", "https://www.gravatar.com/avatar/38e4dbce6996ba5d73c56bee4297d385?s=328&d=identicon&r=PG&f=1", "https://www.gravatar.com/avatar/38e4dbce6996ba5d73c56bee4297d385?s=328&d=identicon&r=PG&f=1"};

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Retain this fragment across configuration changes.
        setRetainInstance(true);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);

        resultGridView = (GridView) view.findViewById(R.id.grid);

        myAdapter = new MyAdapter(getActivity(), urls);
        resultGridView.setAdapter(myAdapter);

        return view;
    }

    public void loadData() {
        resultGridView.invalidateViews();
        myAdapter.notifyDataSetChanged();
    }

}

适配器类

public class MyAdapter extends BaseAdapter {

    private Context context;
    private final LayoutInflater mLayoutInflater;

    private String[] urls;

    public MyAdapter(final Context context, String[] urls) {
        super();
        this.context = context;
        mLayoutInflater = LayoutInflater.from(context);
        this.urls = urls;
    }

    @Override
    public int getCount() {
        return urls.length;
    }

    @Override
    public Object getItem(int i) {
        return urls[i];
    }

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

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.list_item, parent, false);
            vh = new ViewHolder();
            vh.imageView = (ImageView) convertView.findViewById(R.id.image);
            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        Picasso.with(context).load(urls[position]).into(vh.imageView);
        return convertView;
    }

    static class ViewHolder {
        ImageView imageView;
    }
}

1 个答案:

答案 0 :(得分:0)

您是否尝试使用OnSaveInstanceState(Bundle)?

您可以尝试将方法getUrls()和setUrls添加到适配器:

public void getURLs(){
    return urls;
}
public void setURLs(String[] urls){
    this.urls = urls;
}

然后将以下行添加到片段中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mData = savedInstanceState.getStringArray("urls");
}

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putStringArray("urls", (getListAdapter().getURLs()));
    super.onSaveInstanceState(outState);
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    myAdapter.setUrls(mData);
    loadData()
}

此外,您应遵循Android代码样式指南:https://source.android.com/source/code-style.html