RecyclerView不可见

时间:2018-07-18 08:55:19

标签: android view android-recyclerview

我在MainActivity中有此代码

int main() {
#if defined(F)
#if F(0, 2, 0)
  return 0;
#endif
#endif
  return 1;
}

但是我明白了

public class MainActivity extends AppCompatActivity implements MainViewInterface{

RecyclerView rvMovies;

private String TAG = "MainActivity";
MoviesAdapter adapter;
MainPresenter mainPresenter;

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

    setupMVP();
    mainPresenter.getMovies();

    rvMovies = (RecyclerView)findViewById(R.id.rvMovies);

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    rvMovies.setLayoutManager(manager);
    //rvMovies.setHasFixedSize(true);
}

private void setupMVP() {

    mainPresenter = new MainPresenter(this);

}



@Override
public void showToast(String s) {
    Toast.makeText(MainActivity.this,s,Toast.LENGTH_LONG).show();
}

@Override
public void displayMovies(MovieResponse moviesResponse) {

    if(moviesResponse!=null) {
        Log.d(TAG,moviesResponse.getResults().get(1).getTitle());
        adapter = new MoviesAdapter(moviesResponse.getResults(), MainActivity.this);
        rvMovies.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }else{
        Log.d(TAG,"Movies response null");
    }

}

@Override
public void displayError(String s) {
    showToast(s);
  }
}

我知道其他人也问过同样的问题。我尝试了他们的解决方案,但是没有用。我想念什么?

这是我的适配器代码。

E/RecyclerView: No adapter attached; skipping layout

谢谢, 西奥。

已解决

在我的activivity_main.xml中,我既有身高,也等于0dp !!!老实说,我不知道如何以及谁将这些价值观付诸实践!这就是为什么看不到回收站视图的原因!

4 个答案:

答案 0 :(得分:2)

设置视图后,需要设置适配器。

rvMovies = (RecyclerView)findViewById(R.id.rvMovies);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setOrientation(LinearLayoutManager.VERTICAL);
rvMovies.setLayoutManager(manager);
//rvMovies.setHasFixedSize(true);
//call after setting the view
mainPresenter.getMovies(); 

答案 1 :(得分:1)

我认为您应该在rvMovies.setAdapter(new MoviesAdapter(new ArrayList<>()))期间在RecyclerView上调用onCreate-在RecyclerView上设置一个空的(从项目角度而言)适配器没有什么不好,它只会显示一个空列表。然后,一旦准备好数据(从要加载的任何源下载/加载数据),您就可以简单地调用诸如adapter.setItems(newItemsList)adapter.notifyDataSetChanged()之类的东西。

还有一点注意:您不应该将Context存储在类成员字段中,因为可能会发生泄漏(我很确定Android Studio也必须以Lint Warning检查的形式对此进行投诉)。实际上,您甚至不需要将Context实例传递到适配器,因为您可以简单地从Context中的parent视图中检索onCreateViewHolder实例-像这样重写它: / p>

@Override
public MoviesHolder onCreateViewHolder( ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_movies,parent,false);
    MoviesHolder mh = new MoviesHolder(v);
    return mh;
}

答案 2 :(得分:0)

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

    setupMVP();

    rvMovies = (RecyclerView)findViewById(R.id.rvMovies);

    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setOrientation(LinearLayoutManager.VERTICAL);
    rvMovies.setLayoutManager(manager);
    rvMovies.setVisibility(View.VISIBLE);
    mainPresenter.getMovies();  <== put this line in last
}

答案 3 :(得分:0)

这2行:

*ngIf="transaction.id == clickedTransactionId

必须写在以下位置:

setupMVP();
mainPresenter.getMovies();