如何解决ListView放置在片段活动下并通过 ViewPager 显示不显示所有项目
这是第一次在这里发帖。所以我有一个在 Android 上使用 Java 创建的编剧应用程序。它具有如下所示的简单布局。layout of the activity
因此,当用户点击 write 时,他们将能够使用纯文本编写剧本,但遵循称为 Fountain 的特定语法。下面给出一个例子。
如果用户编写以下文本...
作家 (微笑) 我必须这样做。
应用程序会将文本分成几行,并将不同的行添加到列表字符串(listy)中,从那里,当用户点击阅读选项卡时,应用程序将从listy的位置0开始并检查是否item 是一个场景标题、动作或对话,它会根据这些来格式化文本。动作和场景标题重力为“左”,对话重力为“中心”。
This is what the user sees after formatting the screenplay.
问题是格式化后listview没有显示所有项目。就像我上面提到的,我将纯文本拆分为单独的行,并将它们添加到 ListString(listy),然后在确定 ListString(listy) 中的项目是动作、对话或场景标题后,我创建带有“类型”和“内容”键的地图,类型是场景标题、对话或动作,内容是 ListString(listy) 中的项目。
然后我将地图添加到 ListMap(fountain) 并将其设置为列表视图数据。
我遇到的问题是列表视图没有显示所有项目。当我滚动时,有些项目变得可见,而有些则不可见。而且侧面的滚动条不动,一直停留在顶部,好像我根本没有滚动一样。
起初我以为是我的手机(三星 J7 Neo),但我也在三星 S10+ 上测试了该应用,问题仍然存在。
请帮帮我。可能是什么问题?
注意:我使用 Sketchware 来开发我的应用程序,而不是 Android Studio。
这是片段的 XML 代码。
<LinearLayout
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"
android:orientation="vertical">
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:dividerHeight="3dp"
android:choiceMode="none"
android:layout_weight="1"
android:divider="@android:color/transparent"/>
</LinearLayout>
这是 ReadScriptFragmentActivity.java
public class ReadscriptFragmentActivity extends Fragment {
private HashMap<String,Object> map = new HashMap<>();
private String path = "";
private String SCRIPT = "";
private double pos = 0;
private HashMap<String,Object> map2 = new HashMap<>();
private String item1 = "";
private boolean isUpperCaSEOnly = false;
private double pos1 = 0;
private String character = "";
private String item2 = "";
private String typeBind = "";
private ArrayList<String> listy = new ArrayList<>();
private ArrayList<HashMap<String,Object>> fountain = new ArrayList<>();
private ArrayList<String> listy2 = new ArrayList<>();
private ListView lv;
private SharedPreferences sp;
private SharedPreferences script;
private Notification not;
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater _inflater,@Nullable ViewGroup _container,@Nullable Bundle _savedInstanceState) {
View _view = _inflater.inflate(R.layout.readscript_fragment,_container,false);
initialize(_savedInstanceState,_view);
initializeLogic();
return _view;
}
private void initialize(Bundle _savedInstanceState,View _view) {
lv = (ListView) _view.findViewById(R.id.lv);
sp = getContext().getSharedPreferences("sp",Activity.MODE_PRIVATE);
script = getContext().getSharedPreferences("script",Activity.MODE_PRIVATE);
}
private void initializeLogic() {
_getScript();
}
@Override
public void onActivityResult(int _requestCode,int _resultCode,Intent _data) {
super.onActivityResult(_requestCode,_resultCode,_data);
switch (_requestCode) {
default:
break;
}
}
@Override
public void onResume() {
super.onResume();
_getScript();
}
public void _getScript () {
path = script.getString("path","");
if (path.equals("")) {
SketchwareUtil.showMessage(getContext(),"Error opening Script!");
}
else {
if (FileUtil.readFile(path).equals("")) {
SketchwareUtil.showMessage(getContext(),"Error opening Script!");
}
else {
map = new Gson().fromJson(FileUtil.readFile(path),new Typetoken<HashMap<String,Object>>(){}.getType());
SCRIPT = map.get("script").toString();
listy = new ArrayList<String>(Arrays.asList(SCRIPT.split("\n\n")));
lv.setAdapter(new ArrayAdapter<String>(getContext(),android.R.layout.simple_list_item_1,listy));
pos = 0;
for(int _repeat35 = 0; _repeat35 < (int)(listy.size()); _repeat35++) {
item1 = listy.get((int)(pos));
if (item1.contains("\n")) {
item2 = item1.substring((int)(0),(int)(item1.indexOf("\n") - 1));
if (item2.equals(item2.toupperCase())) {
map2 = new HashMap<>();
map2.put("type","dialogue");
map2.put("content"," ".concat(item1));
fountain.add(map2);
}
else {
map2 = new HashMap<>();
map2.put("type","action");
map2.put("content",item1);
fountain.add(map2);
}
}
else {
if (item1.equals(item1.toupperCase())) {
if (item1.startsWith("INT.") || item1.startsWith("EXT.")) {
map2 = new HashMap<>();
map2.put("type","scene");
map2.put("content",item1);
fountain.add(map2);
}
else {
map2 = new HashMap<>();
map2.put("type","action");
map2.put("content",item1);
fountain.add(map2);
}
}
else {
map2 = new HashMap<>();
map2.put("type",item1);
fountain.add(map2);
}
}
pos++;
}
}
}
lv.setAdapter(new LvAdapter(fountain));
}
public class LvAdapter extends BaseAdapter {
ArrayList<HashMap<String,Object>> _data;
public LvAdapter(ArrayList<HashMap<String,Object>> _arr) {
_data = _arr;
}
@Override
public int getCount() {
return _data.size();
}
@Override
public HashMap<String,Object> getItem(int _index) {
return _data.get(_index);
}
@Override
public long getItemId(int _index) {
return _index;
}
@Override
public View getView(final int _position,View _v,ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.scriptreaditem,null);
}
final LinearLayout all = (LinearLayout) _view.findViewById(R.id.all);
final TextView scene = (TextView) _view.findViewById(R.id.scene);
final TextView action = (TextView) _view.findViewById(R.id.action);
final TextView dialogue = (TextView) _view.findViewById(R.id.dialogue);
typeBind = _data.get((int)_position).get("type").toString();
if (typeBind.equals("scene")) {
action.setVisibility(View.GONE);
dialogue.setVisibility(View.GONE);
scene.setText(_data.get((int)_position).get("content").toString());
scene.setTypeface(Typeface.createFromAsset(getContext().getAssets(),"fonts/courier_prime_regular.ttf"),1);
}
if (typeBind.equals("action")) {
scene.setVisibility(View.GONE);
dialogue.setVisibility(View.GONE);
action.setText(_data.get((int)_position).get("content").toString());
action.setTypeface(Typeface.createFromAsset(getContext().getAssets(),0);
}
if (typeBind.equals("dialogue")) {
scene.setVisibility(View.GONE);
action.setVisibility(View.GONE);
dialogue.setText(_data.get((int)_position).get("content").toString());
dialogue.setTypeface(Typeface.createFromAsset(getContext().getAssets(),0);
}
return _view;
}
}
}
请帮忙。我不确定可能是什么问题。我不是开发人员。我通过 Sketchware 学习了 Java。所以请耐心等待我。提前致谢。
编辑:我找到了问题所在。如果您查看 ListView onBindCustomView,我添加了三个 if 语句来检查项目是对话、场景标题还是动作。问题是我为每个语句添加了单独的 if 语句。所以我所做的是把它们放在一个 if-else 语句中,就像如果项目是场景,然后格式化为场景,否则如果项目是动作,格式化为动作,否则如果项目是对话,格式为对话。简而言之,我将其他 if 语句放在上一个 if 语句的“else”部分下。 (对不起,如果我的英语不好)。这是新的更新代码。 公共类 ReadscriptFragmentActivity 扩展 Fragment {
private HashMap<String,Object> map = new HashMap<>();
private String path = "";
private String SCRIPT = "";
private double pos = 0;
private HashMap<String,Object> map2 = new HashMap<>();
private String item1 = "";
private boolean isUpperCaSEOnly = false;
private double pos1 = 0;
private String character = "";
private String item2 = "";
private String typeBind = "";
private ArrayList<String> listy = new ArrayList<>();
private ArrayList<HashMap<String,Object>> fountain = new ArrayList<>();
private ArrayList<String> listy2 = new ArrayList<>();
private ListView lv;
private SharedPreferences sp;
private SharedPreferences script;
private Notification not;
@NonNull
@Override
public View onCreateView(@NonNull LayoutInflater _inflater,@Nullable Bundle _savedInstanceState) {
View _view = _inflater.inflate(R.layout.readscript_fragment,false);
initialize(_savedInstanceState,_view);
initializeLogic();
return _view;
}
private void initialize(Bundle _savedInstanceState,View _view) {
lv = (ListView) _view.findViewById(R.id.lv);
sp = getContext().getSharedPreferences("sp",Activity.MODE_PRIVATE);
script = getContext().getSharedPreferences("script",Activity.MODE_PRIVATE);
}
private void initializeLogic() {
_getScript();
}
@Override
public void onActivityResult(int _requestCode,Intent _data) {
super.onActivityResult(_requestCode,_data);
switch (_requestCode) {
default:
break;
}
}
@Override
public void onResume() {
super.onResume();
_getScript();
}
public void _getScript () {
path = script.getString("path","");
if (path.equals("")) {
SketchwareUtil.showMessage(getContext(),"Error opening Script!");
}
else {
if (FileUtil.readFile(path).equals("")) {
SketchwareUtil.showMessage(getContext(),"Error opening Script!");
}
else {
map = new Gson().fromJson(FileUtil.readFile(path),Object>>(){}.getType());
SCRIPT = map.get("script").toString();
listy = new ArrayList<String>(Arrays.asList(SCRIPT.split("\n\n")));
pos = 0;
for(int _repeat35 = 0; _repeat35 < (int)(listy.size()); _repeat35++) {
item1 = listy.get((int)(pos));
if (item1.contains("\n")) {
item2 = item1.substring((int)(0),(int)(item1.indexOf("\n") - 1));
if (item2.equals(item2.toupperCase())) {
map2 = new HashMap<>();
map2.put("type","dialogue");
map2.put("content"," ".concat(item1));
fountain.add(map2);
}
else {
map2 = new HashMap<>();
map2.put("type","action");
map2.put("content",item1);
fountain.add(map2);
}
}
else {
if (item1.equals(item1.toupperCase())) {
if (item1.startsWith("INT.") || item1.startsWith("EXT.")) {
map2 = new HashMap<>();
map2.put("type","scene");
map2.put("content",item1);
fountain.add(map2);
}
else {
map2 = new HashMap<>();
map2.put("type",item1);
fountain.add(map2);
}
}
else {
map2 = new HashMap<>();
map2.put("type",item1);
fountain.add(map2);
}
}
pos++;
}
}
}
lv.setAdapter(new LvAdapter(fountain));
}
public class LvAdapter extends BaseAdapter {
ArrayList<HashMap<String,Object>> _data;
public LvAdapter(ArrayList<HashMap<String,Object>> _arr) {
_data = _arr;
}
@Override
public int getCount() {
return _data.size();
}
@Override
public HashMap<String,Object> getItem(int _index) {
return _data.get(_index);
}
@Override
public long getItemId(int _index) {
return _index;
}
@Override
public View getView(final int _position,ViewGroup _container) {
LayoutInflater _inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View _view = _v;
if (_view == null) {
_view = _inflater.inflate(R.layout.scriptreaditem,null);
}
final LinearLayout all = (LinearLayout) _view.findViewById(R.id.all);
final TextView scene = (TextView) _view.findViewById(R.id.scene);
final TextView action = (TextView) _view.findViewById(R.id.action);
final TextView dialogue = (TextView) _view.findViewById(R.id.dialogue);
scene.setTypeface(Typeface.createFromAsset(getContext().getAssets(),1);
action.setTypeface(Typeface.createFromAsset(getContext().getAssets(),0);
dialogue.setTypeface(Typeface.createFromAsset(getContext().getAssets(),0);
typeBind = _data.get((int)_position).get("type").toString();
if (typeBind.equals("scene")) {
action.setVisibility(View.GONE);
dialogue.setVisibility(View.GONE);
scene.setVisibility(View.VISIBLE);
scene.setText(_data.get((int)_position).get("content").toString());
}
else {
if (typeBind.equals("action")) {
scene.setVisibility(View.GONE);
dialogue.setVisibility(View.GONE);
action.setVisibility(View.VISIBLE);
action.setText(_data.get((int)_position).get("content").toString());
}
else {
if (typeBind.equals("dialogue")) {
scene.setVisibility(View.GONE);
action.setVisibility(View.GONE);
dialogue.setVisibility(View.VISIBLE);
dialogue.setText(_data.get((int)_position).get("content").toString());
}
}
}
return _view;
}
}
}
感谢回复的人。我想这个问题现在已经有了答案。干杯。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。